package com.css.fxfzypg.manage.lethalitydifference.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.attachment.entity.AttachmentInfoEntity;
import com.css.fxfzypg.base.attachment.service.AttachmentInfoService;
import com.css.fxfzypg.base.exception.ServiceException;
import com.css.fxfzypg.constants.FxfzConstants;
import com.css.fxfzypg.constants.YNEnum;
import com.css.fxfzypg.manage.lethalitydifference.repository.LethalityDifferenceRepository;
import com.css.fxfzypg.manage.lethalitydifference.repository.entity.LethalityDifferenceEntity;
import com.css.fxfzypg.manage.lethalitydifference.repository.entity.LethalityDifferenceFlagEntity;
import com.css.fxfzypg.manage.lethalitydifference.repository.entity.LethalityDifferenceItemEntity;
import com.css.fxfzypg.manage.lethalitydifference.service.LethalityDifferenceService;
import com.css.fxfzypg.manage.lethalitydifference.verify.LethalityDifferenceExcelVerifyHandler;
import com.css.fxfzypg.manage.lethalitydifference.vo.DistrictVo;
import com.css.fxfzypg.manage.lethalitydifference.vo.ExcelVo;
import com.css.fxfzypg.manage.lethalitydifference.vo.LethalityDifferenceParams;
import com.css.fxfzypg.manage.lethalitydifference.vo.SysMenuVo;
import com.css.fxfzypg.modules.SettingSeismic.entity.SeismicIntensityRelation;
import com.css.fxfzypg.modules.SettingSeismic.repository.IntensityRelationRepository;
import com.css.fxfzypg.modules.preassesstaskmanage.entity.PreAssessTaskEntity;
import com.css.fxfzypg.modules.preassesstaskmanage.service.PreAssessTaskService;
import com.css.fxfzypg.modules.surveytaskmanage.entity.FieldSurveyTaskEntity;
import com.css.fxfzypg.modules.surveytaskmanage.repository.FieldSurveyTaskRepository;
import com.css.fxfzypg.modules.surveytaskmanage.service.FieldSurveyTaskService;
import com.css.fxfzypg.modules.surveytaskmanage.vo.TaskVo;
import com.css.fxfzypg.util.*;
import com.css.fxfzypg.web.PlatformSessionContext;
import com.css.fxfzypg.zcpt.sys.entity.SUser;
import com.css.fxfzypg.zcpt.sys.service.SUserService;
import com.css.fxfzypg.zcpt.sys.service.SysAreaService;
import com.css.fxfzypg.zcpt.sys.service.SysRoleService;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.*;

@Slf4j
@Service
public class LethalityDifferenceServiceImpl implements LethalityDifferenceService {

    @Autowired
    LethalityDifferenceRepository lethalityDifferenceRepository;
    @Autowired
    AttachmentInfoService attachmentInfoService;
    @Autowired
    SysAreaService sysAreaService;
    @Autowired
    SUserService sUserService;
    @Autowired
    SysRoleService sysRoleService;
    @Autowired
    FieldSurveyTaskService fieldSurveyTaskService;
    @Autowired
    LethalityDifferenceExcelVerifyHandler lethalityDifferenceExcelVerifyHandler;
    @Autowired
    PreAssessTaskService preAssessTaskService;
    @Autowired
    FieldSurveyTaskRepository fieldSurveyTaskRepository;
    @Resource
    IntensityRelationRepository intensityRelationRepository;

    @Transactional(rollbackFor = Exception.class )
    @Override
    public boolean saveDataByTask(String taskCode, String taskName, String taskDistrict) throws UnsupportedEncodingException {
        List<String> userIds=fieldSurveyTaskService.getGroupUserIdByTaskCode(taskCode);
        String userId = PlatformSessionContext.getUserID();
        SUser sUser = CreateGson.createGson().fromJson(sUserService.getUserById(userId), SUser.class);
        String userName = sUser.getUserName();
        boolean res = false;
        if(!PlatformObjectUtils.isEmpty(taskDistrict)){
            List<LethalityDifferenceEntity> list = new ArrayList<>();
            for(String district : taskDistrict.split("、")){
                int n = district.length() - district.replaceAll("-", "").length();
                List<SysMenuVo> sysMenuList = getSAreaTree("1");//获取所有省
                if (district.contains("北京市")||district.contains("天津市")||district.contains("上海市")||district.contains("重庆市")){
                    if(n==0){
                        for(SysMenuVo sysMenuVo : sysMenuList) {
                            String name = sysMenuVo.getName();
                            if (district.equals(name)) {//省名称
                                List<SysMenuVo> citySysMenu = getSAreaTree(sysMenuVo.getValue());//获取该省下所有市
                                for(SysMenuVo citySysMenuVo : citySysMenu){
                                    List<SysMenuVo> areaSysMenu = getSAreaTree(citySysMenuVo.getValue());//获取市下所有区县
                                    for(SysMenuVo areaSysMenuVo : areaSysMenu){
                                        LethalityDifferenceEntity lethalityDifferenceEntity = new LethalityDifferenceEntity();
                                        String uuid = UUIDGenerator.getUUID();
                                        lethalityDifferenceEntity.setId(uuid);
                                        lethalityDifferenceEntity.setTaskCode(taskCode);
                                        lethalityDifferenceEntity.setTaskName(taskName);
                                        lethalityDifferenceEntity.setProvince(district);
                                        lethalityDifferenceEntity.setProvinceId(sysMenuVo.getValue());
                                        lethalityDifferenceEntity.setCity(citySysMenuVo.getName());
                                        lethalityDifferenceEntity.setCityId(citySysMenuVo.getValue());
                                        lethalityDifferenceEntity.setArea(areaSysMenuVo.getName());
                                        lethalityDifferenceEntity.setAreaId(areaSysMenuVo.getValue());
                                        lethalityDifferenceEntity.setIsSubmit("0");
                                        lethalityDifferenceEntity.setIsAdopt("0");
                                        lethalityDifferenceEntity.setDelFlag("0");
                                        lethalityDifferenceEntity.setCreateId(userId);
                                        lethalityDifferenceEntity.setCreateName(userName);
                                        lethalityDifferenceEntity.setCreateTime(new Date());
                                        lethalityDifferenceEntity.setGeom(getDivisionCodeByAreaId(lethalityDifferenceEntity.getAreaId()));
                                        list.add(lethalityDifferenceEntity);
                                    }
                                }
                            }
                        }
                    }else if(n==1){
                        String province = district.split("-")[0];
                        String city = district.split("-")[0];
                        String area = district.split("-")[1];
                        String divisionIds = sysAreaService.getIdByCondition(URLEncoder.encode(province, "UTF-8"), URLEncoder.encode(city, "UTF-8"), URLEncoder.encode(area, "UTF-8"));
                        LethalityDifferenceEntity lethalityDifferenceEntity = new LethalityDifferenceEntity();
                        String uuid = UUIDGenerator.getUUID();
                        lethalityDifferenceEntity.setId(uuid);
                        lethalityDifferenceEntity.setTaskCode(taskCode);
                        lethalityDifferenceEntity.setTaskName(taskName);
                        lethalityDifferenceEntity.setProvince(province);
                        lethalityDifferenceEntity.setProvinceId(divisionIds.split("_")[0]);
                        lethalityDifferenceEntity.setCity(city);
                        lethalityDifferenceEntity.setCityId(divisionIds.split("_")[1]);
                        lethalityDifferenceEntity.setArea(area);
                        lethalityDifferenceEntity.setAreaId(divisionIds.split("_")[2]);
                        lethalityDifferenceEntity.setIsSubmit("0");
                        lethalityDifferenceEntity.setIsAdopt("0");
                        lethalityDifferenceEntity.setDelFlag("0");
                        lethalityDifferenceEntity.setCreateId(userId);
                        lethalityDifferenceEntity.setCreateName(userName);
                        lethalityDifferenceEntity.setCreateTime(new Date());
                        lethalityDifferenceEntity.setGeom(getDivisionCodeByAreaId(lethalityDifferenceEntity.getAreaId()));
                        list.add(lethalityDifferenceEntity);
                    }
                }else{
                    if(n==0){//说明只有省级,直辖市除外
                        for(SysMenuVo sysMenuVo : sysMenuList){//遍历所有省
                            String name = sysMenuVo.getName();
                            if(district.equals(name)){//省名称
                                String value = sysMenuVo.getValue();//省id
                                List<SysMenuVo> citySysMenu = getSAreaTree(value);//获取该省下所有市
                                for(SysMenuVo citySysMenuVo : citySysMenu){
                                    String cityValue = citySysMenuVo.getValue();//市id
                                    List<SysMenuVo> areaSysMenu = getSAreaTree(cityValue);//获取市下所有区县
                                    for(SysMenuVo areaSysMenuVo : areaSysMenu){
                                        LethalityDifferenceEntity lethalityDifferenceEntity = new LethalityDifferenceEntity();
                                        String uuid = UUIDGenerator.getUUID();
                                        lethalityDifferenceEntity.setId(uuid);
                                        lethalityDifferenceEntity.setTaskCode(taskCode);
                                        lethalityDifferenceEntity.setTaskName(taskName);
                                        lethalityDifferenceEntity.setProvince(district);
                                        lethalityDifferenceEntity.setProvinceId(value);
                                        lethalityDifferenceEntity.setCity(citySysMenuVo.getName());
                                        lethalityDifferenceEntity.setCityId(citySysMenuVo.getValue());
                                        lethalityDifferenceEntity.setArea(areaSysMenuVo.getName());
                                        lethalityDifferenceEntity.setAreaId(areaSysMenuVo.getValue());
                                        lethalityDifferenceEntity.setIsSubmit("0");
                                        lethalityDifferenceEntity.setIsAdopt("0");
                                        lethalityDifferenceEntity.setDelFlag("0");
                                        lethalityDifferenceEntity.setCreateId(userId);
                                        lethalityDifferenceEntity.setCreateName(userName);
                                        lethalityDifferenceEntity.setCreateTime(new Date());
                                        lethalityDifferenceEntity.setGeom(getDivisionCodeByAreaId(lethalityDifferenceEntity.getAreaId()));
                                        list.add(lethalityDifferenceEntity);
                                    }
                                }
                            }
                        }
                    }else if(n==1){//有省市两级
                        String province = district.split("-")[0];//省
                        String city = district.split("-")[1];//市
                        for(SysMenuVo sysMenuVo : sysMenuList) {//遍历所有省
                            String provinceName = sysMenuVo.getName();
                            if (province.equals(sysMenuVo.getName())) {//省名称
                                List<SysMenuVo> citySysMenu = getSAreaTree(sysMenuVo.getValue());//获取该省下所有市
                                for(SysMenuVo citySysMenuVo : citySysMenu) {
                                    if(city.equals(citySysMenuVo.getName())){
                                        List<SysMenuVo> areaSysMenu = getSAreaTree(citySysMenuVo.getValue());//获取该省下所有区县
                                        for(SysMenuVo areaSysMenuVo : areaSysMenu){
                                            LethalityDifferenceEntity lethalityDifferenceEntity = new LethalityDifferenceEntity();
                                            String uuid = UUIDGenerator.getUUID();
                                            lethalityDifferenceEntity.setId(uuid);
                                            lethalityDifferenceEntity.setTaskCode(taskCode);
                                            lethalityDifferenceEntity.setTaskName(taskName);
                                            lethalityDifferenceEntity.setProvince(provinceName);
                                            lethalityDifferenceEntity.setProvinceId(sysMenuVo.getValue());
                                            lethalityDifferenceEntity.setCity(city);
                                            lethalityDifferenceEntity.setCityId(citySysMenuVo.getValue());
                                            lethalityDifferenceEntity.setArea(areaSysMenuVo.getName());
                                            lethalityDifferenceEntity.setAreaId(areaSysMenuVo.getValue());
                                            lethalityDifferenceEntity.setIsSubmit("0");
                                            lethalityDifferenceEntity.setIsAdopt("0");
                                            lethalityDifferenceEntity.setDelFlag("0");
                                            lethalityDifferenceEntity.setCreateId(userId);
                                            lethalityDifferenceEntity.setCreateName(userName);
                                            lethalityDifferenceEntity.setCreateTime(new Date());
                                            lethalityDifferenceEntity.setGeom(getDivisionCodeByAreaId(lethalityDifferenceEntity.getAreaId()));
                                            list.add(lethalityDifferenceEntity);
                                        }
                                    }
                                }
                            }
                        }
                    }else if(n==2){
                        String province = district.split("-")[0];//省
                        String city = district.split("-")[1];//市
                        String area = district.split("-")[2];//区县
                        String divisionIds = sysAreaService.getIdByCondition(URLEncoder.encode(province, "UTF-8"), URLEncoder.encode(city, "UTF-8"), URLEncoder.encode(area, "UTF-8"));
                        LethalityDifferenceEntity lethalityDifferenceEntity = new LethalityDifferenceEntity();
                        String uuid = UUIDGenerator.getUUID();
                        lethalityDifferenceEntity.setId(uuid);
                        lethalityDifferenceEntity.setTaskCode(taskCode);
                        lethalityDifferenceEntity.setTaskName(taskName);
                        lethalityDifferenceEntity.setProvince(province);
                        lethalityDifferenceEntity.setProvinceId(divisionIds.split("_")[0]);
                        lethalityDifferenceEntity.setCity(city);
                        lethalityDifferenceEntity.setCityId(divisionIds.split("_")[1]);
                        lethalityDifferenceEntity.setArea(area);
                        lethalityDifferenceEntity.setAreaId(divisionIds.split("_")[2]);
                        lethalityDifferenceEntity.setIsSubmit("0");
                        lethalityDifferenceEntity.setIsAdopt("0");
                        lethalityDifferenceEntity.setDelFlag("0");
                        lethalityDifferenceEntity.setCreateId(userId);
                        lethalityDifferenceEntity.setCreateName(userName);
                        lethalityDifferenceEntity.setCreateTime(new Date());
                        lethalityDifferenceEntity.setGeom(getDivisionCodeByAreaId(lethalityDifferenceEntity.getAreaId()));
                        list.add(lethalityDifferenceEntity);
                    }
                }
            }
            for(int i=0;i<list.size();i++){
                LethalityDifferenceEntity lethalityDifferenceEntity = list.get(i);
                res = lethalityDifferenceRepository.save(lethalityDifferenceEntity);
                if(!res){
                    break;
                }
            }
            List<LethalityDifferenceEntity> appList = new ArrayList<>();
            for (LethalityDifferenceEntity lethalityDifferenceEntity : list) {
                for (String id : userIds) {
                    LethalityDifferenceEntity entity=new LethalityDifferenceEntity();
                    BeanUtils.copyProperties(lethalityDifferenceEntity,entity);
                    entity.setId(UUIDGenerator.getUUID());
                    entity.setCreateId(id);
                    entity.setDelFlag("2");
                    appList.add(entity);
                }
            }
            for (LethalityDifferenceEntity lethalityDifferenceEntity : appList) {
                lethalityDifferenceRepository.save(lethalityDifferenceEntity);
            }

        }
        return res;
    }


    private static LethalityDifferenceEntity add(String taskCode,String taskName,String userId,String userName){
        LethalityDifferenceEntity lethalityDifferenceEntity = new LethalityDifferenceEntity();
        String uuid = UUIDGenerator.getUUID();
        lethalityDifferenceEntity.setId(uuid);
        lethalityDifferenceEntity.setTaskCode(taskCode);
        lethalityDifferenceEntity.setTaskName(taskName);
        lethalityDifferenceEntity.setProvince("");
        lethalityDifferenceEntity.setProvinceId("");
        lethalityDifferenceEntity.setCity("");
        lethalityDifferenceEntity.setCityId("");
        lethalityDifferenceEntity.setArea("");
        lethalityDifferenceEntity.setAreaId("");
        lethalityDifferenceEntity.setCreateId(userId);
        lethalityDifferenceEntity.setCreateName(userName);
        lethalityDifferenceEntity.setCreateTime(new Date());
        return lethalityDifferenceEntity;
    }


    @Transactional(rollbackFor = Exception.class )
    @Override
    public boolean save(LethalityDifferenceEntity lethalityDifferenceEntity) throws UnsupportedEncodingException {
        String uuid = UUIDGenerator.getUUID();
        lethalityDifferenceEntity.setId(uuid);
         /* lethalityDifferenceEntity.setProvince(sysAreaService.getAreaNameByAreaId(URLEncoder.encode(lethalityDifferenceEntity.getProvinceId(),"UTF-8")));
        lethalityDifferenceEntity.setCity(sysAreaService.getAreaNameByAreaId(URLEncoder.encode(lethalityDifferenceEntity.getCityId(),"UTF-8")));
        lethalityDifferenceEntity.setArea(sysAreaService.getAreaNameByAreaId(URLEncoder.encode(lethalityDifferenceEntity.getAreaId(),"UTF-8")));
*/
        String divisionIds = sysAreaService.getIdByCondition(URLEncoder.encode(lethalityDifferenceEntity.getProvince(), "UTF-8"), URLEncoder.encode(lethalityDifferenceEntity.getCity(), "UTF-8"), URLEncoder.encode(lethalityDifferenceEntity.getArea(), "UTF-8"));
        lethalityDifferenceEntity.setProvinceId(divisionIds.split("_")[0]);
        lethalityDifferenceEntity.setCityId(divisionIds.split("_")[1]);
        lethalityDifferenceEntity.setAreaId(divisionIds.split("_")[2]);
        lethalityDifferenceEntity.setCreateTime(new Date());
        lethalityDifferenceEntity.setIsSubmit("0");
        lethalityDifferenceEntity.setIsAdopt("0");
        lethalityDifferenceEntity.setDelFlag("0");
        lethalityDifferenceEntity.setGeom(getDivisionCodeByAreaId(lethalityDifferenceEntity.getAreaId()));
        List<LethalityDifferenceItemEntity> list = lethalityDifferenceEntity.getCustomIndicatorList();
        boolean main = lethalityDifferenceRepository.save(lethalityDifferenceEntity);
        if(!PlatformObjectUtils.isEmpty(list)) {
            Boolean res = false;
            if (main) {
                for (int i = 0; i < list.size(); i++) {
                    LethalityDifferenceItemEntity lethalityDifferenceItemEntity = list.get(i);
                    lethalityDifferenceItemEntity.setId(UUIDGenerator.getUUID());
                    lethalityDifferenceItemEntity.setMainId(uuid);
                    lethalityDifferenceItemEntity.setDelFlag("0");
                    res = lethalityDifferenceRepository.saveItem(lethalityDifferenceItemEntity);
                    if (!res) {
                        break;
                    }
                }
            }
            return res;
        }
        return main;
    }

    @Transactional(rollbackFor = Exception.class )
    @Override
    public boolean saveAndSubmit(LethalityDifferenceEntity lethalityDifferenceEntity) throws UnsupportedEncodingException {
        String uuid = UUIDGenerator.getUUID();
        lethalityDifferenceEntity.setId(uuid);
        String divisionIds = sysAreaService.getIdByCondition(URLEncoder.encode(lethalityDifferenceEntity.getProvince(), "UTF-8"), URLEncoder.encode(lethalityDifferenceEntity.getCity(), "UTF-8"), URLEncoder.encode(lethalityDifferenceEntity.getArea(), "UTF-8"));
        lethalityDifferenceEntity.setProvinceId(divisionIds.split("_")[0]);
        lethalityDifferenceEntity.setCityId(divisionIds.split("_")[1]);
        lethalityDifferenceEntity.setAreaId(divisionIds.split("_")[2]);
        lethalityDifferenceEntity.setCreateTime(new Date());
        lethalityDifferenceEntity.setSubmitTime(new Date());
        lethalityDifferenceEntity.setIsSubmit("1");
        lethalityDifferenceEntity.setIsAdopt("0");
        lethalityDifferenceEntity.setDelFlag("0");
        lethalityDifferenceEntity.setGeom(getDivisionCodeByAreaId(lethalityDifferenceEntity.getAreaId()));
        List<LethalityDifferenceItemEntity> list = lethalityDifferenceEntity.getCustomIndicatorList();
        boolean mainSave = lethalityDifferenceRepository.save(lethalityDifferenceEntity);
        if(!PlatformObjectUtils.isEmpty(list)) {
            Boolean itemSave = false;
            if (mainSave) {
                for (int i = 0; i < list.size(); i++) {
                    LethalityDifferenceItemEntity lethalityDifferenceItemEntity = list.get(i);
                    lethalityDifferenceItemEntity.setId(UUIDGenerator.getUUID());
                    lethalityDifferenceItemEntity.setMainId(uuid);
                    lethalityDifferenceItemEntity.setDelFlag("0");
                    itemSave = lethalityDifferenceRepository.saveItem(lethalityDifferenceItemEntity);
                    if (!itemSave) {
                        break;
                    }
                }
            }
            return itemSave;
        }
        return mainSave;
    }

    @Override
    public boolean saveAndSubmitPC(LethalityDifferenceEntity lethalityDifferenceEntity) throws UnsupportedEncodingException {
        String uuid = UUIDGenerator.getUUID();
        lethalityDifferenceEntity.setId(uuid);
        lethalityDifferenceEntity.setProvince(sysAreaService.getAreaNameByAreaId(URLEncoder.encode(lethalityDifferenceEntity.getProvinceId(),"UTF-8")));
        lethalityDifferenceEntity.setCity(sysAreaService.getAreaNameByAreaId(URLEncoder.encode(lethalityDifferenceEntity.getCityId(),"UTF-8")));
        lethalityDifferenceEntity.setArea(sysAreaService.getAreaNameByAreaId(URLEncoder.encode(lethalityDifferenceEntity.getAreaId(),"UTF-8")));
        lethalityDifferenceEntity.setCreateTime(new Date());
        lethalityDifferenceEntity.setSubmitTime(new Date());
        lethalityDifferenceEntity.setIsSubmit("1");
        lethalityDifferenceEntity.setIsAdopt("0");
        lethalityDifferenceEntity.setDelFlag("0");
        lethalityDifferenceEntity.setGeom(getDivisionCodeByAreaId(lethalityDifferenceEntity.getAreaId()));
        List<LethalityDifferenceItemEntity> list = lethalityDifferenceEntity.getCustomIndicatorList();
        boolean mainSave = lethalityDifferenceRepository.save(lethalityDifferenceEntity);
        if(!PlatformObjectUtils.isEmpty(list)) {
            Boolean itemSave = false;
            if (mainSave) {
                for (int i = 0; i < list.size(); i++) {
                    LethalityDifferenceItemEntity lethalityDifferenceItemEntity = list.get(i);
                    lethalityDifferenceItemEntity.setId(UUIDGenerator.getUUID());
                    lethalityDifferenceItemEntity.setMainId(uuid);
                    lethalityDifferenceItemEntity.setDelFlag("0");
                    itemSave = lethalityDifferenceRepository.saveItem(lethalityDifferenceItemEntity);
                    if (!itemSave) {
                        break;
                    }
                }
            }
            return itemSave;
        }
        return mainSave;
    }

    @Transactional(rollbackFor = Exception.class )
    @Override
    public boolean deleteCustomIndicatorById(String id) {
        return lethalityDifferenceRepository.deleteCustomIndicatorById(id);
    }

    @Transactional(rollbackFor = Exception.class )
    @Override
    public boolean modifyLethalityDifference(LethalityDifferenceEntity lethalityDifferenceEntity) {
        boolean mainModify = lethalityDifferenceRepository.modifyLethalityDifference(lethalityDifferenceEntity);
        if(mainModify){
            List<LethalityDifferenceItemEntity> list = lethalityDifferenceEntity.getCustomIndicatorList();
            boolean itemModify = true;
            if(!PlatformObjectUtils.isEmpty(list)) {
                itemModify = false;
                for (int i = 0; i < list.size(); i++) {
                    LethalityDifferenceItemEntity lethalityDifferenceItemEntity = list.get(i);
                    if(PlatformObjectUtils.isEmpty(lethalityDifferenceItemEntity.getId())){
                        lethalityDifferenceItemEntity.setId(UUIDGenerator.getUUID());
                        lethalityDifferenceItemEntity.setMainId(lethalityDifferenceEntity.getId());
                        lethalityDifferenceItemEntity.setDelFlag("0");
                        itemModify = lethalityDifferenceRepository.saveItem(lethalityDifferenceItemEntity);
                        if(!itemModify) {
                            break;
                        }
                    }else{
                        itemModify = lethalityDifferenceRepository.modifyLethalityDifferenceItem(lethalityDifferenceItemEntity);
                        if(!itemModify){
                            break;
                        }
                    }
                }
            }
            return itemModify;
        }
        return mainModify;
    }

    @Transactional(rollbackFor = Exception.class )
    @Override
    public boolean modifyAndSubmit(LethalityDifferenceEntity lethalityDifferenceEntity) {
        lethalityDifferenceEntity.setIsSubmit("1");
        lethalityDifferenceEntity.setSubmitTime(new Date());
        boolean mainModify = lethalityDifferenceRepository.modifyLethalityDifference(lethalityDifferenceEntity);
        if(mainModify){
            List<LethalityDifferenceItemEntity> list = lethalityDifferenceEntity.getCustomIndicatorList();
            boolean itemModify = false;
            if(!PlatformObjectUtils.isEmpty(list)) {
                for (int i = 0; i < list.size(); i++) {
                    LethalityDifferenceItemEntity lethalityDifferenceItemEntity = list.get(i);
                    if(PlatformObjectUtils.isEmpty(lethalityDifferenceItemEntity.getId())){
                        lethalityDifferenceItemEntity.setId(UUIDGenerator.getUUID());
                        lethalityDifferenceItemEntity.setMainId(lethalityDifferenceEntity.getId());
                        lethalityDifferenceItemEntity.setDelFlag("0");
                        itemModify = lethalityDifferenceRepository.saveItem(lethalityDifferenceItemEntity);
                        if(!itemModify) {
                            break;
                        }
                    }else{
                        itemModify = lethalityDifferenceRepository.modifyLethalityDifferenceItem(lethalityDifferenceItemEntity);
                        if(!itemModify){
                            break;
                        }
                    }
                }
                return itemModify;
            }
        }
        return mainModify;
    }

    @Override
    public Map<String, Object> querySubmitOrNot(String submitFlag, String taskCode, int curPage, int pageSize) {
        return lethalityDifferenceRepository.querySubmitOrNot(submitFlag, taskCode,curPage,pageSize);
    }

    @Transactional(rollbackFor = Exception.class )
    @Override
    public boolean updateIsSubmit(Map<String, String> map) {
        return lethalityDifferenceRepository.updateIsSubmit(map);
    }

    @Override
    public JSONObject queryData(String id) {
        JSONObject json = new JSONObject();
        LethalityDifferenceEntity lethalityDifferenceEntity = lethalityDifferenceRepository.queryData(id);

        //获取建筑物附件
        List<AttachmentInfoEntity> attachmentInfoEntityList = attachmentInfoService.queryFile(lethalityDifferenceEntity.getBuildingFile());
        json.put("buildingFile",attachmentInfoEntityList);
        //人口附件
        List<AttachmentInfoEntity> attachmentInfoEntityList1 = attachmentInfoService.queryFile(lethalityDifferenceEntity.getPeopleNumFile());
        json.put("peopleNumFile",attachmentInfoEntityList1);
        //地形地貌附件
        List<AttachmentInfoEntity> attachmentInfoEntityList2 = attachmentInfoService.queryFile(lethalityDifferenceEntity.getTerrainFile());
        json.put("terrainFile",attachmentInfoEntityList2);
        //道路交通附件
        List<AttachmentInfoEntity> attachmentInfoEntityList3 = attachmentInfoService.queryFile(lethalityDifferenceEntity.getTrafficFile());
        json.put("trafficFile",attachmentInfoEntityList3);
        //气候附件
        List<AttachmentInfoEntity> attachmentInfoEntityList4 = attachmentInfoService.queryFile(lethalityDifferenceEntity.getClimateFile());
        json.put("climateFile",attachmentInfoEntityList4);
        //自定义附件
        List<LethalityDifferenceItemEntity> itemList = new ArrayList<>();
        List<LethalityDifferenceItemEntity> list = lethalityDifferenceRepository.queryItemData(id);
        if(!PlatformObjectUtils.isEmpty(list)) {
            for(int i=0;i<list.size();i++){
                LethalityDifferenceItemEntity lethalityDifferenceItemEntity = list.get(i);
                String customIndicatorFile = lethalityDifferenceItemEntity.getCustomIndicatorFile();
                List<AttachmentInfoEntity> fileList = new ArrayList<>();
                if(!PlatformObjectUtils.isEmpty(customIndicatorFile)) {
                    for (String fileId : customIndicatorFile.split(",")) {
                        List<AttachmentInfoEntity> attachmentInfoEntities = attachmentInfoService.queryFile(fileId);
                        fileList.addAll(attachmentInfoEntities);
                        //lethalityDifferenceItemEntity.setCustomFileList(attachmentInfoEntities);
                    }
                    //itemList.add(lethalityDifferenceItemEntity.setCustomFileList(fileList));
                }
                if(!PlatformObjectUtils.isEmpty(fileList)){
                    lethalityDifferenceItemEntity.setCustomFileList(fileList);
                }else{
                    lethalityDifferenceItemEntity.setCustomFileList(null);
                }
                itemList.add(lethalityDifferenceItemEntity);
            }
        }
        lethalityDifferenceEntity.setCustomIndicatorList(itemList);
        json.put("data",lethalityDifferenceEntity);
        return json;
    }

    @Override
    public Map<String, Object> queryDataList(HttpServletRequest request, LethalityDifferenceParams lethalityDifferenceParams, int curPage, int pageSize) {
        System.err.println("获取区县致死性列表数据-------");
        String userId = PlatformSessionContext.getUserID();
        String user = sUserService.getSUser(userId);
        SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
        String role = sysRoleService.getRoleByUserId(userId);
        List<Map> maps = JSONObject.parseArray(role, Map.class);//一个人可能有多个角色
        //当前登录人所有的角色
        StringBuilder roles = new StringBuilder();
        for(int i=0;i<maps.size();i++){
            Map map = maps.get(i);
            roles.append(map.get("roleCode")+",");
        }
        String allRole = roles.toString();
        String provinceId = sUser.getProvinceId();//省id
        //获取所有任务编号和任务管理员
        List<TaskVo> taskManagers = fieldSurveyTaskService.getTaskManagers();
        //当前登录人(任务管理员)新建的所有任务
        List<String> taskCodeList = new ArrayList<>();
        if(!PlatformObjectUtils.isEmpty(taskManagers)) {
            for (TaskVo taskVo : taskManagers) {
                String createUser = taskVo.getCreateUser();
                if (createUser.equals(userId)) {
                    taskCodeList.add(taskVo.getTaskNum());
                }
            }
        }
        //国家管理员和业务员有可能 是组长 组员,不可能是省管理员 省业务员
        //省级管理员和业务员有可能 是组长 组员,不可能是国家管理员  国家业务员,也有可能参与了其他省的任务,需要显示省数据和他参与的
        //国家管理员 查看  修改  删除
        if(allRole.contains(FxfzConstants.YPG_COUN_GL_ROLE)){
            System.err.println("登录人:"+userId+",角色是 国家管理员------");
            Map<String, Object> map = lethalityDifferenceRepository.queryDataList(request,lethalityDifferenceParams,curPage,pageSize);
            List<LethalityDifferenceParams> lethalityParams = (List<LethalityDifferenceParams>) map.get("rows");
            List<LethalityDifferenceParams> list = new ArrayList<>();
            if(lethalityParams != null && lethalityParams.size()>0){
                for(LethalityDifferenceParams lethalityData : lethalityParams){
                    lethalityData.setFlag("2");
                    list.add(lethalityData);
                }
            }
            map.put("rows",duplicateRemova2(list));
            return  map;
        }
        //国家业务员  查看 (如果是本人创建的或者组长需要加修改删除)
        else if(allRole.contains(FxfzConstants.YPG_COUN_YW_ROLE)){
            System.err.println("登录人:"+userId+",角色是 国家业务员------");
            List<LethalityDifferenceParams> list = new ArrayList<>();
            Map<String, Object> map = lethalityDifferenceRepository.queryDataList(request,lethalityDifferenceParams,curPage,pageSize);
            List<LethalityDifferenceParams> lethalityParams = (List<LethalityDifferenceParams>) map.get("rows");
            if(lethalityParams != null && lethalityParams.size()>0) {
                for (LethalityDifferenceParams lethalityData : lethalityParams) {
                    String createId = lethalityData.getCreateId();
                    String groupLeaderId = fieldSurveyTaskService.getGroupLeaderId(lethalityData.getTaskCode());//组长
                    if(PlatformObjectUtils.isEmpty(groupLeaderId)){
                        groupLeaderId="无组长";
                    }
                    if (userId.equals(createId)) {
                        System.err.println("登录人:"+userId+",角色是 国家业务员------本人创建");
                        lethalityData.setFlag("2");
                    } else if (groupLeaderId.equals(userId)) {
                        System.err.println("登录人:"+userId+",角色是 国家业务员------组长");
                        lethalityData.setFlag("2");
                    } else {
                        System.err.println("登录人:"+userId+",角色是 国家业务员------");
                        lethalityData.setFlag("1");
                    }
                    //任务管理员
                    if(taskCodeList.contains(lethalityData.getTaskCode())){
                        lethalityData.setFlag("2");
                    }
                    if (lethalityData.getFlag() == null) {
                        lethalityData.setFlag("1");
                    }
                    list.add(lethalityData);
                }
            }

            map.put("rows",duplicateRemova2(list));
            return map;
        }
        //省级管理员(各省查看各省的)  查看  修改  删除
        else if(allRole.contains(FxfzConstants.YPG_PRI_GL_ROLE)){
            System.err.println("登录人:"+userId+",角色是 省级管理员------");
            //获取该省所有数据和管理员参与的
            Map<String, Object> map = lethalityDifferenceRepository.queryLethalityDifferenceDataByProvince(lethalityDifferenceParams,curPage,pageSize,userId,provinceId,taskCodeList);
            List<LethalityDifferenceParams> list = new ArrayList<>();
            List<LethalityDifferenceParams> lethalityParams = (List<LethalityDifferenceParams>) map.get("rows");
            if(lethalityParams != null && lethalityParams.size() > 0) {
                for (LethalityDifferenceParams lethalityData : lethalityParams) {
                    //判断该业务员参与的其他省的  是否为组员
                    List<Map<String, Object>> teamMembers = fieldSurveyTaskService.getTeamMembers(lethalityData.getTaskCode());//组员
                    if (!PlatformObjectUtils.isEmpty(teamMembers) && teamMembers.size() > 0) {
                        for (Map<String, Object> map2 : teamMembers) {
                            String teamMember = map2.get("user_id").toString();
                            if (!PlatformObjectUtils.isEmpty(teamMember)) {
                                if (userId.equals(teamMember)) {
                                    System.err.println("登录人:"+userId+",任务编号:"+lethalityData.getTaskCode()+",该任务中的组员------");
                                    lethalityData.setFlag("1");
                                    break;
                                }
                            }
                        }
                    }
                    //判断该业务员参与的其他省的    或者自己参与的是否为组长或本人
                    String createId = lethalityData.getCreateId();
                    String groupLeaderId = fieldSurveyTaskService.getGroupLeaderId(lethalityData.getTaskCode());//组长
                    if(PlatformObjectUtils.isEmpty(groupLeaderId)){
                        groupLeaderId="无组长";
                    }
                    if (userId.equals(createId) || groupLeaderId.equals(createId)) {
                        lethalityData.setFlag("2");
                    }
                    //判断是否为自己省
                    if (lethalityData.getProvinceId() != null && lethalityData.getProvinceId().equals(provinceId)) {
                        lethalityData.setFlag("2");
                    }
                    //任务管理员
                    if(taskCodeList.contains(lethalityData.getTaskCode())){
                        lethalityData.setFlag("2");
                    }
                    if (lethalityData.getFlag() == null) {
                        lethalityData.setFlag("1");
                    }
                    list.add(lethalityData);
                }
            }
            map.put("rows", duplicateRemova2(list));
            return map;
        }
        //省级业务员(各省查看各省的)查看   如果是本人创建的或者组长需要加修改删除)
        else if(allRole.contains(FxfzConstants.YPG_PRI_YW_ROLE)){
            System.err.println("登录人:"+userId+",角色是 省级业务员------");
            //获取该省所有数据和业务员参与的
            Map<String, Object> map = lethalityDifferenceRepository.queryLethalityDifferenceDataByProvince(lethalityDifferenceParams,curPage,pageSize,userId,provinceId,taskCodeList);
            List<LethalityDifferenceParams> list = new ArrayList<>();
            List<LethalityDifferenceParams> lethalityParams = (List<LethalityDifferenceParams>) map.get("rows");
            if(lethalityParams != null && lethalityParams.size() > 0) {
                for (LethalityDifferenceParams lethalityData : lethalityParams) {
                    //判断是否为自己省
                    if (lethalityData.getProvinceId() != null && lethalityData.getProvinceId().equals(provinceId)) {
                        lethalityData.setFlag("1");
                    }
                    //判断该业务员参与的其他省的  是否为组员
                    List<Map<String, Object>> teamMembers = fieldSurveyTaskService.getTeamMembers(lethalityData.getTaskCode());//组员
                    if (!PlatformObjectUtils.isEmpty(teamMembers) && teamMembers.size() > 0) {
                        for (Map<String, Object> map2 : teamMembers) {
                            String teamMember = map2.get("user_id").toString();
                            if (!PlatformObjectUtils.isEmpty(teamMember)) {
                                if (userId.equals(teamMember)) {
                                    System.err.println("登录人:"+userId+",任务编号:"+lethalityData.getTaskCode()+",该任务中的组员------");
                                    lethalityData.setFlag("1");
                                    break;
                                }
                            }
                        }
                    }
                    //判断该业务员参与的其他省的    或者自己参与的是否为组长或本人
                    String createId = lethalityData.getCreateId();
                    String groupLeaderId = fieldSurveyTaskService.getGroupLeaderId(lethalityData.getTaskCode());//组长
                    if(PlatformObjectUtils.isEmpty(groupLeaderId)){
                        groupLeaderId="无组长";
                    }
                    if (userId.equals(createId) || groupLeaderId.equals(createId)) {
                        lethalityData.setFlag("2");
                    }
                    //任务管理员
                    if(taskCodeList.contains(lethalityData.getTaskCode())){
                        lethalityData.setFlag("2");
                    }
                    if (lethalityData.getFlag() == null) {
                        lethalityData.setFlag("1");
                    }
                    list.add(lethalityData);
                }
            }
            map.put("rows", duplicateRemova2(list));
            return map;
        }
        //本人创建的或者组长:查看 修改 删除,或者是小组组员:查看
        System.err.println("登录人:"+userId+",无国家/省角色------");
        Map<String, Object> map = lethalityDifferenceRepository.getLethalityDifferenceDataByTeamMembers(request,lethalityDifferenceParams,curPage,pageSize,userId,taskCodeList);
        List<LethalityDifferenceParams> list = new ArrayList<>();
        List<LethalityDifferenceParams> LethalityParams = (List<LethalityDifferenceParams>) map.get("rows");
        if(LethalityParams != null && LethalityParams.size()>0){
            for(LethalityDifferenceParams lethalityData:LethalityParams){
                //获取某任务下所有组员
                List<Map<String, Object>> teamMembers = fieldSurveyTaskService.getTeamMembers(lethalityData.getTaskCode());//组员
                if(!PlatformObjectUtils.isEmpty(teamMembers) && teamMembers.size()>0) {
                    for (Map<String, Object> map2 : teamMembers) {
                        String teamMember = map2.get("user_id").toString();
                        if (!PlatformObjectUtils.isEmpty(teamMember)) {
                            if (userId.equals(teamMember)) {
                                System.err.println("登录人:"+userId+",任务编号:"+lethalityData.getTaskCode()+",该任务中的组员------");
                                lethalityData.setFlag("1");
                                break;
                            }
                        }
                    }
                }
                String groupLeaderId = fieldSurveyTaskService.getGroupLeaderId(lethalityData.getTaskCode());//组长
                if(PlatformObjectUtils.isEmpty(groupLeaderId)){
                    groupLeaderId="无组长";
                }

                //本人或者组长
                if(userId.equals(lethalityData.getCreateId()) || userId.equals(groupLeaderId)){
                    System.err.println("登录人:"+userId+",本人创建或者组长------");
                    lethalityData.setFlag("2");
                }
                if (lethalityData.getFlag() == null) {
                    lethalityData.setFlag("1");
                }
                list.add(lethalityData);
            }
        }
        map.put("rows",duplicateRemova2(list));
        return map;
    }

    @Transactional(rollbackFor = Exception.class )
    @Override
    public Map<String,Object> updateIsAdopt(Map<String,String> map) {
        String id = map.get("id");
        String isAdopt = map.get("isAdopt");
        Map<String,Object> resMap = new HashMap();
        //采纳
        if(isAdopt.equals("1")) {
            //检查相同任务、相同区县是否已有被采纳的数据，
            //如有则提示“一个区县只能指定一组被采纳的指标值，如需变更，请先取消采纳本任务相同区县的数据。”
            Map<String, Object> dataByTaskCodeAndArea = lethalityDifferenceRepository.getDataByTaskCodeAndArea(id, isAdopt);
            if (dataByTaskCodeAndArea != null && dataByTaskCodeAndArea.size() > 0) {
                int count = Integer.parseInt(dataByTaskCodeAndArea.get("count").toString());
                if (count > 0) {
                    resMap.put("res", false);
                    resMap.put("message", "一个区县只能指定一组被采纳的指标值，如需变更，请先取消采纳本任务相同区县的数据。");
                } else {
                    boolean res = lethalityDifferenceRepository.updateIsAdopt(id, isAdopt);
                    resMap.put("res", res);
                }
            }
        }else if(isAdopt.equals("0")){  //取消采纳
            boolean res = lethalityDifferenceRepository.updateIsAdopt(id, isAdopt);
            resMap.put("res",res);
        }
        return resMap;
    }

    @Transactional(rollbackFor = Exception.class )
    @Override
    public boolean deleteById(String id) {
        boolean res = lethalityDifferenceRepository.deleteById(id);
        List<LethalityDifferenceItemEntity> lethalityDifferenceItemEntities = lethalityDifferenceRepository.queryItemData(id);
        if(!PlatformObjectUtils.isEmpty(lethalityDifferenceItemEntities)){
            if(res){
                res = lethalityDifferenceRepository.deleteItemById(id);
            }
        }
        return res;
    }

    @Override
    public Map<String, Object> queryPutDataList(HttpServletRequest request, LethalityDifferenceParams lethalityDifferenceParams, int curPage, int pageSize) {
        String userId = PlatformSessionContext.getUserID();
        String user = sUserService.getSUser(userId);
        SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
        String role = sysRoleService.getRoleByUserId(userId);
        List<Map> maps = JSONObject.parseArray(role, Map.class);//一个人可能有多个角色
        //当前登录人所有的角色
        StringBuilder roles = new StringBuilder();
        for(int i=0;i<maps.size();i++){
            Map map = maps.get(i);
            roles.append(map.get("roleCode")+",");
        }
        String allRole = roles.toString();
        String provinceId = sUser.getProvinceId();//省id
        //国家级业务人员可维护全国区县的致死性差异性指标的权重值。
        if(allRole.contains(FxfzConstants.YPG_COUN_GL_ROLE) || allRole.contains(FxfzConstants.YPG_COUN_YW_ROLE)){
            return lethalityDifferenceRepository.queryPutDataList(request,lethalityDifferenceParams,curPage,pageSize);
        }
        //各省业务人员可在本模块中维护区县致死性差异性指标的权重值。
        if(allRole.contains(FxfzConstants.YPG_PRI_GL_ROLE) || allRole.contains(FxfzConstants.YPG_PRI_YW_ROLE)) {
            lethalityDifferenceParams.setProvinceId(provinceId);
            return lethalityDifferenceRepository.queryPutDataList(request, lethalityDifferenceParams, curPage, pageSize);
        }
        return null;
    }

    @Override
    public JSONObject getDataById(String id) {
        List<Object> list = new ArrayList<>();
        LethalityDifferenceEntity lethalityDifferenceEntity = lethalityDifferenceRepository.queryData(id);
        List<LethalityDifferenceItemEntity> itemList = lethalityDifferenceRepository.queryItemData(id);
        //获取建筑物附件
        //List<AttachmentInfoEntity> attachmentInfoEntityList = attachmentInfoService.queryFile(lethalityDifferenceEntity.getBuildingFile());
        List<Object> buildingFileList = new ArrayList<>();
        String buildingFile = lethalityDifferenceEntity.getBuildingFile();
        if(!PlatformObjectUtils.isEmpty(buildingFile)) {
            for (String fileId : buildingFile.split(",")) {
                List<AttachmentInfoEntity> attachmentInfoEntities = attachmentInfoService.queryFile(fileId);
                //lethalityDifferenceItemEntity.setCustomFileList(attachmentInfoEntities);
                buildingFileList.addAll(attachmentInfoEntities);
            }
        }
        Map<String, Object> buildingMap = new HashMap<>();
        buildingMap.put("indexName","建筑物");
        buildingMap.put("value",lethalityDifferenceEntity.getBuilding());
        buildingMap.put("weightValue",lethalityDifferenceEntity.getBuildingWeightValue());
        buildingMap.put("explain",lethalityDifferenceEntity.getBuildingExplain());
        buildingMap.put("file",buildingFileList);
        list.add(buildingMap);

        //人口附件
        //List<AttachmentInfoEntity> attachmentInfoEntityList1 = attachmentInfoService.queryFile(lethalityDifferenceEntity.getPeopleNumFile());
        List<Object> peopleNumFileList = new ArrayList<>();
        String peopleNumFile = lethalityDifferenceEntity.getPeopleNumFile();
        if(!PlatformObjectUtils.isEmpty(peopleNumFile)) {
            for (String fileId : peopleNumFile.split(",")) {
                List<AttachmentInfoEntity> attachmentInfoEntities = attachmentInfoService.queryFile(fileId);
                //lethalityDifferenceItemEntity.setCustomFileList(attachmentInfoEntities);
                peopleNumFileList.addAll(attachmentInfoEntities);
            }
        }
        Map<String, Object> peopleNumMap = new HashMap<>();
        peopleNumMap.put("indexName","人口");
        peopleNumMap.put("value",lethalityDifferenceEntity.getPeopleNum());
        peopleNumMap.put("weightValue",lethalityDifferenceEntity.getPeopleNumWeightValue());
        peopleNumMap.put("explain",lethalityDifferenceEntity.getPeopleNumExplain());
        peopleNumMap.put("file",peopleNumFileList);
        list.add(peopleNumMap);

        //地形地貌附件
        //List<AttachmentInfoEntity> attachmentInfoEntityList2 = attachmentInfoService.queryFile(lethalityDifferenceEntity.getTerrainFile());
        List<Object> terrainFileList = new ArrayList<>();
        String terrainFile = lethalityDifferenceEntity.getTerrainFile();
        if(!PlatformObjectUtils.isEmpty(terrainFile)) {
            for (String fileId : terrainFile.split(",")) {
                List<AttachmentInfoEntity> attachmentInfoEntities = attachmentInfoService.queryFile(fileId);
                //lethalityDifferenceItemEntity.setCustomFileList(attachmentInfoEntities);
                terrainFileList.addAll(attachmentInfoEntities);
            }
        }
        Map<String, Object> terrainMap = new HashMap<>();
        terrainMap.put("indexName","地形地貌");
        terrainMap.put("value",lethalityDifferenceEntity.getTerrain());
        terrainMap.put("weightValue",lethalityDifferenceEntity.getTerrainWeightValue());
        terrainMap.put("explain",lethalityDifferenceEntity.getTerrainExplain());
        terrainMap.put("file",terrainFileList);
        list.add(terrainMap);

        //道路交通附件
        List<Object> trafficFileList = new ArrayList<>();
        String trafficFile = lethalityDifferenceEntity.getTrafficFile();
        if(!PlatformObjectUtils.isEmpty(trafficFile)) {
            for (String fileId : trafficFile.split(",")) {
                List<AttachmentInfoEntity> attachmentInfoEntities = attachmentInfoService.queryFile(fileId);
                trafficFileList.addAll(attachmentInfoEntities);
            }
        }
        Map<String, Object> trafficMap = new HashMap<>();
        trafficMap.put("indexName","道路交通");
        trafficMap.put("value",lethalityDifferenceEntity.getTraffic());
        trafficMap.put("weightValue",lethalityDifferenceEntity.getTrafficWeightValue());
        trafficMap.put("explain",lethalityDifferenceEntity.getTrafficExplain());
        trafficMap.put("file",trafficFileList);
        list.add(trafficMap);

        //气候附件
        List<Object> climateFileList = new ArrayList<>();
        String climateFile = lethalityDifferenceEntity.getClimateFile();
        if(!PlatformObjectUtils.isEmpty(climateFile)) {
            for (String fileId : climateFile.split(",")) {
                List<AttachmentInfoEntity> attachmentInfoEntities = attachmentInfoService.queryFile(fileId);
                //lethalityDifferenceItemEntity.setCustomFileList(attachmentInfoEntities);
                climateFileList.addAll(attachmentInfoEntities);
            }
        }
        Map<String, Object> climateMap = new HashMap<>();
        climateMap.put("indexName","气候");
        climateMap.put("value",lethalityDifferenceEntity.getClimate());
        climateMap.put("weightValue",lethalityDifferenceEntity.getClimateWeightValue());
        climateMap.put("explain",lethalityDifferenceEntity.getClimateExplain());
        climateMap.put("file",climateFileList);
        list.add(climateMap);

        //自定义附件1
        if(!PlatformObjectUtils.isEmpty(itemList)){
            for (int i=0;i<itemList.size();i++){
                Map<String, Object> customIndicatorMap = new HashMap<>();
                LethalityDifferenceItemEntity lethalityDifferenceItemEntity = itemList.get(i);
                String customIndicatorFile = lethalityDifferenceItemEntity.getCustomIndicatorFile();
                List<Object> fileList = new ArrayList<>();
                if(!PlatformObjectUtils.isEmpty(customIndicatorFile)) {
                    for (String fileId : customIndicatorFile.split(",")) {
                        List<AttachmentInfoEntity> attachmentInfoEntities = attachmentInfoService.queryFile(fileId);
                        //lethalityDifferenceItemEntity.setCustomFileList(attachmentInfoEntities);
                        fileList.addAll(attachmentInfoEntities);
                    }
                }
                customIndicatorMap.put("itemId",lethalityDifferenceItemEntity.getId());
                customIndicatorMap.put("indexName",lethalityDifferenceItemEntity.getCustomIndicatorName());
                customIndicatorMap.put("value",lethalityDifferenceItemEntity.getCustomIndicatorValue());
                customIndicatorMap.put("weightValue",lethalityDifferenceItemEntity.getCustomIndicatorWeightValue());
                customIndicatorMap.put("explain",lethalityDifferenceItemEntity.getCustomIndicatorExplain());
                customIndicatorMap.put("file",fileList);
                list.add(customIndicatorMap);
            }
        }
        JSONObject json = new JSONObject();
        json.put("entity",lethalityDifferenceEntity);
        json.put("table",list);
        return json;
    }

    @Transactional(rollbackFor = Exception.class )
    @Override
    public boolean addWeightValue(Map<String, Object> map) {
        LethalityDifferenceEntity lethalityDifferenceEntity = new LethalityDifferenceEntity();
        lethalityDifferenceEntity.setId((String) map.get("id"));
        List<Map<String,Object>> list = (List<Map<String, Object>>) map.get("dataList");
        for(int i = 0;i < list.size();i++){
            Map<String, Object> map1 = list.get(i);
           if(i==0){
                lethalityDifferenceEntity.setBuildingWeightValue((String) map1.get("weightValue"));
            }
            if(i==1){
                lethalityDifferenceEntity.setPeopleNumWeightValue((String) map1.get("weightValue"));
            }
            if(i==2){
                lethalityDifferenceEntity.setTerrainWeightValue((String) map1.get("weightValue"));
            }
            if(i==3){
                lethalityDifferenceEntity.setTrafficWeightValue((String) map1.get("weightValue"));
            }
            if(i==4){
                lethalityDifferenceEntity.setClimateWeightValue((String) map1.get("weightValue"));
            }
        }
        lethalityDifferenceEntity.setMaintainer((String) map.get("maintainer"));
        lethalityDifferenceEntity.setMaintainerId((String) map.get("maintainerId"));
        lethalityDifferenceEntity.setMaintainerDepartment((String) map.get("maintainerDepartment"));
        lethalityDifferenceEntity.setMaintainTime(new Date());
        boolean mainRes = lethalityDifferenceRepository.addWeightValue(lethalityDifferenceEntity);
        if(mainRes){
            boolean res = false;
            for(int i = 5;i < list.size();i++){
                Map<String, Object> map1 = list.get(i);
                String itemId = (String) map1.get("itemId");
                String weightValue = (String) map1.get("weightValue");
                res = lethalityDifferenceRepository.addItemWeightValue(itemId,weightValue);
                if(!res){
                    return res;
                }
            }
        }
        return mainRes;
    }

    @Transactional(rollbackFor = Exception.class )
    @Override
    public Map<Object, Object> createFinalValue(String taskDistrict, String taskNum) throws UnsupportedEncodingException {
        PreAssessTaskEntity task = preAssessTaskService.getPreAssessTaskByTaskNum(taskNum);
        SeismicIntensityRelation byTaskId = intensityRelationRepository.getByTaskId(task.getId());
        byTaskId.setButton9_status1("1");
        intensityRelationRepository.updateSeismicIntensityRelation(byTaskId);

        Map<Object, Object> map = new HashMap<>();
        String lethalityIds = "";
        if(!PlatformObjectUtils.isEmpty(taskDistrict)){
            List<DistrictVo> list = new ArrayList<>();
            for(String district : taskDistrict.split("、")){
                int n = district.length() - district.replaceAll("-", "").length();
                List<SysMenuVo> sysMenuList = getSAreaTree("1");//获取所有省
                if (district.contains("北京市")||district.contains("天津市")||district.contains("上海市")||district.contains("重庆市")){
                    if(n==0){
                        for(SysMenuVo sysMenuVo : sysMenuList) {
                            String name = sysMenuVo.getName();
                            if (district.equals(name)) {//省名称
                                List<SysMenuVo> citySysMenu = getSAreaTree(sysMenuVo.getValue());//获取该省下所有市
                                for(SysMenuVo citySysMenuVo : citySysMenu){
                                    List<SysMenuVo> areaSysMenu = getSAreaTree(citySysMenuVo.getValue());//获取市下所有区县
                                    for(SysMenuVo areaSysMenuVo : areaSysMenu){
                                        DistrictVo districtVo = new DistrictVo();
                                        districtVo.setProvince(sysMenuVo.getName());
                                        districtVo.setCity(citySysMenuVo.getName());
                                        districtVo.setArea(areaSysMenuVo.getName());
                                        districtVo.setProvinceId(sysMenuVo.getValue());
                                        districtVo.setCityId(citySysMenuVo.getValue());
                                        districtVo.setAreaId(areaSysMenuVo.getValue());
                                        list.add(districtVo);
                                    }
                                }
                            }
                        }
                    }else if(n==1){
                        String province = district.split("-")[0];
                        String city = district.split("-")[0];
                        String area = district.split("-")[1];
                        String divisionIds = sysAreaService.getIdByCondition(URLEncoder.encode(province, "UTF-8"), URLEncoder.encode(city, "UTF-8"), URLEncoder.encode(area, "UTF-8"));
                        DistrictVo districtVo = new DistrictVo();
                        districtVo.setProvince(province);
                        districtVo.setCity(city);
                        districtVo.setArea(area);
                        districtVo.setProvinceId(divisionIds.split("_")[0]);
                        districtVo.setCityId(divisionIds.split("_")[1]);
                        districtVo.setAreaId(divisionIds.split("_")[2]);
                        list.add(districtVo);
                    }
                }else{
                    if(n==0){//说明只有省级,直辖市除外
                        for(SysMenuVo sysMenuVo : sysMenuList){//遍历所有省
                            String name = sysMenuVo.getName();
                            if(district.equals(name)){//省名称
                                String value = sysMenuVo.getValue();//省id
                                List<SysMenuVo> citySysMenu = getSAreaTree(value);//获取该省下所有市
                                for(SysMenuVo citySysMenuVo : citySysMenu){
                                    String cityValue = citySysMenuVo.getValue();//市id
                                    List<SysMenuVo> areaSysMenu = getSAreaTree(cityValue);//获取市下所有区县
                                    for(SysMenuVo areaSysMenuVo : areaSysMenu){
                                        DistrictVo districtVo = new DistrictVo();
                                        districtVo.setProvince(sysMenuVo.getName());
                                        districtVo.setCity(citySysMenuVo.getName());
                                        districtVo.setArea(areaSysMenuVo.getName());
                                        districtVo.setProvinceId(sysMenuVo.getValue());
                                        districtVo.setCityId(citySysMenuVo.getValue());
                                        districtVo.setAreaId(areaSysMenuVo.getValue());
                                        list.add(districtVo);
                                    }
                                }
                            }
                        }
                    }else if(n==1){//有省市两级
                        String province = district.split("-")[0];//省
                        String city = district.split("-")[1];//市
                        for(SysMenuVo sysMenuVo : sysMenuList) {//遍历所有省
                            String provinceName = sysMenuVo.getName();
                            if (province.equals(sysMenuVo.getName())) {//省名称
                                List<SysMenuVo> citySysMenu = getSAreaTree(sysMenuVo.getValue());//获取该省下所有市
                                for(SysMenuVo citySysMenuVo : citySysMenu) {
                                    if(city.equals(citySysMenuVo.getName())){
                                        List<SysMenuVo> areaSysMenu = getSAreaTree(citySysMenuVo.getValue());//获取该省下所有区县
                                        for(SysMenuVo areaSysMenuVo : areaSysMenu){
                                            DistrictVo districtVo = new DistrictVo();
                                            districtVo.setProvince(province);
                                            districtVo.setCity(city);
                                            districtVo.setArea(areaSysMenuVo.getName());
                                            districtVo.setProvinceId(sysMenuVo.getValue());
                                            districtVo.setCityId(citySysMenuVo.getValue());
                                            districtVo.setAreaId(areaSysMenuVo.getValue());
                                            list.add(districtVo);
                                        }
                                    }
                                }
                            }
                        }
                    }else if(n==2){
                        String province = district.split("-")[0];//省
                        String city = district.split("-")[1];//市
                        String area = district.split("-")[2];//区县
                        String divisionIds = sysAreaService.getIdByCondition(URLEncoder.encode(province, "UTF-8"), URLEncoder.encode(city, "UTF-8"), URLEncoder.encode(area, "UTF-8"));
                        DistrictVo districtVo = new DistrictVo();
                        districtVo.setProvince(province);
                        districtVo.setCity(city);
                        districtVo.setArea(area);
                        districtVo.setProvinceId(divisionIds.split("_")[0]);
                        districtVo.setCityId(divisionIds.split("_")[1]);
                        districtVo.setAreaId(divisionIds.split("_")[2]);
                        list.add(districtVo);
                    }
                }
            }
            //获取区县致死性所有已入库的数据,根据省市区id
            List<LethalityDifferenceEntity> lethalityDifferenceEntities = new ArrayList<>();
            List<DistrictVo> list2 = new ArrayList<>();//放入没有入库的数据
            for(int i=0;i<list.size();i++){
                DistrictVo districtVo = list.get(i);
                LethalityDifferenceEntity lethalityDifferenceEntity = lethalityDifferenceRepository.getPutData(districtVo.getProvinceId(),districtVo.getCityId(),districtVo.getAreaId());
                if(!PlatformObjectUtils.isEmpty(lethalityDifferenceEntity)){
                    if(districtVo.getProvinceId().equals(lethalityDifferenceEntity.getProvinceId()) && districtVo.getCityId().equals(lethalityDifferenceEntity.getCityId()) && districtVo.getAreaId().equals(lethalityDifferenceEntity.getAreaId())){
                        lethalityDifferenceEntities.add(lethalityDifferenceEntity);
                    }
                }else{
                    DistrictVo districtVo2 = new DistrictVo();
                    districtVo2.setProvince(districtVo.getProvince());
                    districtVo2.setProvinceId(districtVo.getProvinceId());
                    districtVo2.setCity(districtVo.getCity());
                    districtVo2.setCityId(districtVo.getCityId());
                    districtVo2.setArea(districtVo.getArea());
                    districtVo2.setAreaId(districtVo.getAreaId());
                    list2.add(districtVo2);
                }
            }
            StringBuilder messAge = new StringBuilder();
            if(!PlatformObjectUtils.isEmpty(list2)){
                for(DistrictVo districtVo : list2){
                    messAge.append(districtVo.getProvince()+"、"+districtVo.getCity()+"、"+districtVo.getArea() +"还未入库, 不能生成区县致死性差异性分析;\n");
                }
                map.put("mess",messAge.toString());
                map.put("res",false);
                return map;

            }

        Double finalValue = 0.0;
        String mess = "致死性差异性指标权重未赋值”请到【模型管理-致死性差异性指标权重】中赋值。";
        List<LethalityDifferenceEntity> dataList = new ArrayList<>();
        if(!PlatformObjectUtils.isEmpty(lethalityDifferenceEntities)){
            for(int i=0;i<lethalityDifferenceEntities.size();i++){
                LethalityDifferenceEntity entity = new LethalityDifferenceEntity();
                LethalityDifferenceEntity lethalityDifferenceEntity = lethalityDifferenceEntities.get(i);
                String province = lethalityDifferenceEntity.getProvince();
                String city = lethalityDifferenceEntity.getCity();
                String area = lethalityDifferenceEntity.getArea();
                String str = province+"、"+city+"、"+area;
                String buildingWeightValue = lethalityDifferenceEntity.getBuildingWeightValue();
                String peopleNumWeightValue = lethalityDifferenceEntity.getPeopleNumWeightValue();
                String terrainWeightValue = lethalityDifferenceEntity.getTerrainWeightValue();
                String trafficWeightValue = lethalityDifferenceEntity.getTrafficWeightValue();
                String climateWeightValue = lethalityDifferenceEntity.getClimateWeightValue();
                if(PlatformObjectUtils.isEmpty(buildingWeightValue) || PlatformObjectUtils.isEmpty(peopleNumWeightValue) || PlatformObjectUtils.isEmpty(terrainWeightValue) || PlatformObjectUtils.isEmpty(trafficWeightValue) || PlatformObjectUtils.isEmpty(climateWeightValue)){
                    //mess = "“"+str+mess;
                    messAge.append("“"+str+mess);
                    break;
                }else{
                    String building = lethalityDifferenceEntity.getBuilding();
                    String peopleNum = lethalityDifferenceEntity.getPeopleNum();
                    String terrain = lethalityDifferenceEntity.getTerrain();
                    String traffic = lethalityDifferenceEntity.getTraffic();
                    String climate = lethalityDifferenceEntity.getClimate();
                    double mulBuilding = mul(building, buildingWeightValue);
                    double mulPeopleNum = mul(peopleNum, peopleNumWeightValue);
                    double mulTerrain = mul(terrain, terrainWeightValue);
                    double mulTraffic = mul(traffic, trafficWeightValue);
                    double mulClimate = mul(climate, climateWeightValue);
                    finalValue = mulBuilding+mulPeopleNum+mulTerrain+mulTraffic+mulClimate;
                }
                List<LethalityDifferenceItemEntity> lethalityDifferenceItemEntities = lethalityDifferenceRepository.queryItemData(lethalityDifferenceEntity.getId());
                if(!PlatformObjectUtils.isEmpty(lethalityDifferenceItemEntities)){
                    for(LethalityDifferenceItemEntity itemEntity : lethalityDifferenceItemEntities){
                        String customIndicatorWeightValue = itemEntity.getCustomIndicatorWeightValue();
                        String customIndicatorValue = itemEntity.getCustomIndicatorValue();
                        if(PlatformObjectUtils.isEmpty(customIndicatorWeightValue)){
                            //mess = "“"+str+mess;
                            messAge.append("“"+str+mess);
                            break;
                        }else{
                            finalValue+=mul(customIndicatorValue,customIndicatorWeightValue);
                        }
                    }
                }
                entity.setId(lethalityDifferenceEntity.getId());
                entity.setFinalValue(division(finalValue));//除以100,四舍五入保留两位小数
                dataList.add(entity);
            }

                boolean res = false;
                if(!PlatformObjectUtils.isEmpty(dataList)){
                    for(int i = 0;i<dataList.size();i++){
                        LethalityDifferenceEntity lethalityDifferenceEntity = dataList.get(i);
                        lethalityIds += lethalityDifferenceEntity.getId()+",";
                        res = lethalityDifferenceRepository.createFinalValue(lethalityDifferenceEntity);
                    }
                }
                map.put("mess",messAge.toString());
                map.put("res",res);
                //生成成功后,记录已成功
                if(res) {
                    List<LethalityDifferenceFlagEntity> lethalityDifferenceFlagEntityList = lethalityDifferenceRepository.getDataByTaskCode(taskNum);
                    if (PlatformObjectUtils.isEmpty(lethalityDifferenceFlagEntityList) || lethalityDifferenceFlagEntityList.size() == 0) {
                        LethalityDifferenceFlagEntity lethalityDifferenceFlagEntity = new LethalityDifferenceFlagEntity();
                        lethalityDifferenceFlagEntity.setId(UUIDGenerator.getUUID());
                        lethalityDifferenceFlagEntity.setTaskCode(taskNum);
                        lethalityIds = lethalityIds.substring(0, lethalityIds.length() - 1);
                        lethalityDifferenceFlagEntity.setLethalityIds(lethalityIds);
                        lethalityDifferenceFlagEntity.setCreateFinalValueFlag("1");
                        lethalityDifferenceRepository.saveYpgAreaLethalityDifferenceFlag(lethalityDifferenceFlagEntity);
                    }
                }
                return map;
            }else{
                map.put("mess",taskDistrict+"相关的区县还未入库, 不能生成区县致死性差异性分析。");
                map.put("res",false);
                return map;
            }
        }
        map.put("res",true);
        //生成成功后,记录已成功
        String taskCode = "";
        List<LethalityDifferenceFlagEntity> lethalityDifferenceFlagEntityList = lethalityDifferenceRepository.getDataByTaskCode(taskCode);
        if(PlatformObjectUtils.isEmpty(lethalityDifferenceFlagEntityList) || lethalityDifferenceFlagEntityList.size() == 0){
            LethalityDifferenceFlagEntity lethalityDifferenceFlagEntity = new LethalityDifferenceFlagEntity();
            lethalityDifferenceFlagEntity.setId(UUIDGenerator.getUUID());
            lethalityDifferenceFlagEntity.setTaskCode(taskCode);
            lethalityIds = lethalityIds.substring(0,lethalityIds.length()-1);
            lethalityDifferenceFlagEntity.setLethalityIds(lethalityIds);
            lethalityDifferenceFlagEntity.setCreateFinalValueFlag("1");
            lethalityDifferenceRepository.saveYpgAreaLethalityDifferenceFlag(lethalityDifferenceFlagEntity);
        }
        return map;

    }

    public String division(Double a){
        if(a ==0 ){
            return "0";
        }
        double rateStr = (new BigDecimal(a/100).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        return String.valueOf(rateStr);
    }

    public double add(String s1,String s2){
        BigDecimal b1 = BigDecimal.valueOf(Double.valueOf(s1));
        BigDecimal b2 = BigDecimal.valueOf(Double.valueOf(s2));
        return b1.add(b2).doubleValue();
    }

    public double mul(String s1,String s2){
        BigDecimal b1 = BigDecimal.valueOf(Double.valueOf(s1));
        BigDecimal b2 = BigDecimal.valueOf(Double.valueOf(s2));
        return b1.multiply(b2).doubleValue();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Map<String, Object> uploadLethalityDifference(MultipartFile file) throws Exception {
        ImportParams params = new ImportParams();
        //params.setImportFields(new String[]{"*任务编号","*省","*市","*区/县","*建筑物指标值","*说明","*人口指标值","*说明","*地形地貌指标值","*说明","*道路交通指标值","*说明","*气候指标值","*说明","自定义指标名称","自定义指标值","自定义说明","自定义指标名称","自定义指标值","自定义说明","自定义指标名称","自定义指标值","自定义说明","自定义指标名称","自定义指标值","自定义说明","自定义指标名称","自定义指标值","自定义说明"});
        params.setNeedVerify(true);
        params.setVerifyHandler(lethalityDifferenceExcelVerifyHandler);
        boolean res = false;
        Map<String, Object> map = new HashMap<>();
        try {
            String userId = PlatformSessionContext.getUserID();
            String user = sUserService.getSUser(userId);
            SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
            String userName = sUser.getUserName();
            Date date = new Date();
            InputStream inputStream = file.getInputStream();
            ExcelImportResult<ExcelVo> result = ExcelImportUtil.importExcelMore(inputStream, ExcelVo.class, params);
            List<ExcelVo> failList = result.getFailList();
            if (PlatformObjectUtils.isNotEmpty(failList)) {
                StringBuilder errorMsg = new StringBuilder("<font size='5px' color='red'>导入失败：</font>");
                for(ExcelVo excelVo : failList){
                    errorMsg.append("<font size='4px' ><br>").append("第").append(excelVo.getRowNum() + 1).append("行： ").append(excelVo.getErrorMsg()).append("</font>");
                }
                map.put("res",res);
                map.put("mess",errorMsg);
                return map;
            }
            List<ExcelVo> resultData = result.getList();//解析excel
            if (PlatformObjectUtils.isNotEmpty(resultData)) {
                int excelReadRowIndex = 1;
                List<LethalityDifferenceEntity> list = new ArrayList<>();
                for (ExcelVo excelVo : resultData) {
                    if(PlatformObjectUtils.isEmpty(excelVo.getTaskCode()) && PlatformObjectUtils.isEmpty(excelVo.getProvince()) && PlatformObjectUtils.isEmpty(excelVo.getCity()) && PlatformObjectUtils.isEmpty(excelVo.getArea())
                            && PlatformObjectUtils.isEmpty(excelVo.getBuilding()) && PlatformObjectUtils.isEmpty(excelVo.getBuildingExplain())
                            && PlatformObjectUtils.isEmpty(excelVo.getPeopleNum()) && PlatformObjectUtils.isEmpty(excelVo.getPeopleNumExplain())
                            && PlatformObjectUtils.isEmpty(excelVo.getTerrain()) && PlatformObjectUtils.isEmpty(excelVo.getTerrainExplain())
                            && PlatformObjectUtils.isEmpty(excelVo.getTraffic()) && PlatformObjectUtils.isEmpty(excelVo.getTrafficExplain())
                            && PlatformObjectUtils.isEmpty(excelVo.getClimate()) && PlatformObjectUtils.isEmpty(excelVo.getClimateExplain())){
                        continue;
                    }
                    List<LethalityDifferenceItemEntity> itemList = new ArrayList<>();
                    LethalityDifferenceEntity lethalityDifferenceEntity = new LethalityDifferenceEntity();
                    SpringUtil.copyPropertiesIgnoreNull(excelVo, lethalityDifferenceEntity);
                    String uuid = UUIDGenerator.getUUID();
                    lethalityDifferenceEntity.setId(uuid);
                    String divisionIds = sysAreaService.getIdByCondition(URLEncoder.encode(lethalityDifferenceEntity.getProvince(), "UTF-8"), URLEncoder.encode(lethalityDifferenceEntity.getCity(), "UTF-8"), URLEncoder.encode(lethalityDifferenceEntity.getArea(), "UTF-8"));
                    if (StringUtils.isBlank(divisionIds)) {
                        throw new ServiceException("第" + excelReadRowIndex + "行的建筑地点-省(" + lethalityDifferenceEntity.getProvince().trim() + ")有误，请填写准确的名称，例如：北京市");
                    }
                    lethalityDifferenceEntity.setProvinceId(divisionIds.split("_")[0]);
                    if (divisionIds.split("_").length == 1) {
                        throw new ServiceException("第" + excelReadRowIndex + "行的建筑地点-市(" + lethalityDifferenceEntity.getCity().trim() + ")有误，请填写准确的名称，例如：北京市");
                    }
                    lethalityDifferenceEntity.setCityId(divisionIds.split("_")[1]);
                    if (divisionIds.split("_").length == 2) {
                        throw new ServiceException("第" + excelReadRowIndex + "行的建筑地点-县(" + lethalityDifferenceEntity.getArea().trim() + ")有误，请填写准确的名称，例如：北京市");
                    }
//                    lethalityDifferenceEntity.setProvinceId(divisionIds.split("_")[0]);
//                    lethalityDifferenceEntity.setCityId(divisionIds.split("_")[1]);
                    lethalityDifferenceEntity.setAreaId(divisionIds.split("_")[2]);
                    String taskName = fieldSurveyTaskService.getTaskNameByTaskNum(lethalityDifferenceEntity.getTaskCode());
                    lethalityDifferenceEntity.setTaskName(taskName);
                    lethalityDifferenceEntity.setIsSubmit("1");
                    lethalityDifferenceEntity.setIsAdopt("0");
                    lethalityDifferenceEntity.setDelFlag("0");
                    lethalityDifferenceEntity.setCreateName(userName);
                    lethalityDifferenceEntity.setCreateId(userId);
                    lethalityDifferenceEntity.setCreateTime(date);
                    lethalityDifferenceEntity.setSubmitter(userName);
                    lethalityDifferenceEntity.setSubmitterId(userId);
                    lethalityDifferenceEntity.setSubmitTime(date);

                    LethalityDifferenceItemEntity itemEntity1 = new LethalityDifferenceItemEntity();
                    if(!PlatformObjectUtils.isEmpty(excelVo.getCustomIndicatorName1()) && !PlatformObjectUtils.isEmpty(excelVo.getCustomIndicatorValue1())){
                        itemEntity1.setCustomIndicatorName(excelVo.getCustomIndicatorName1());
                        itemEntity1.setCustomIndicatorValue(excelVo.getCustomIndicatorValue1());
                        itemEntity1.setCustomIndicatorExplain(excelVo.getCustomIndicatorExplain1());
                        itemList.add(itemEntity1);
                    }
                    if(!PlatformObjectUtils.isEmpty(excelVo.getCustomIndicatorName2()) && !PlatformObjectUtils.isEmpty(excelVo.getCustomIndicatorValue2())) {
                        LethalityDifferenceItemEntity itemEntity2 = new LethalityDifferenceItemEntity();
                        itemEntity2.setCustomIndicatorName(excelVo.getCustomIndicatorName2());
                        itemEntity2.setCustomIndicatorValue(excelVo.getCustomIndicatorValue2());
                        itemEntity2.setCustomIndicatorExplain(excelVo.getCustomIndicatorExplain2());
                        itemList.add(itemEntity2);
                    }
                    if(!PlatformObjectUtils.isEmpty(excelVo.getCustomIndicatorName3()) && !PlatformObjectUtils.isEmpty(excelVo.getCustomIndicatorValue3())) {
                        LethalityDifferenceItemEntity itemEntity3 = new LethalityDifferenceItemEntity();
                        itemEntity3.setCustomIndicatorName(excelVo.getCustomIndicatorName3());
                        itemEntity3.setCustomIndicatorValue(excelVo.getCustomIndicatorValue3());
                        itemEntity3.setCustomIndicatorExplain(excelVo.getCustomIndicatorExplain3());
                        itemList.add(itemEntity3);
                    }
                    if(!PlatformObjectUtils.isEmpty(excelVo.getCustomIndicatorName4()) && !PlatformObjectUtils.isEmpty(excelVo.getCustomIndicatorValue4())) {
                        LethalityDifferenceItemEntity itemEntity4 = new LethalityDifferenceItemEntity();
                        itemEntity4.setCustomIndicatorName(excelVo.getCustomIndicatorName4());
                        itemEntity4.setCustomIndicatorValue(excelVo.getCustomIndicatorValue4());
                        itemEntity4.setCustomIndicatorExplain(excelVo.getCustomIndicatorExplain4());
                        itemList.add(itemEntity4);
                    }

                    if(!PlatformObjectUtils.isEmpty(excelVo.getCustomIndicatorName5()) && !PlatformObjectUtils.isEmpty(excelVo.getCustomIndicatorValue5())) {
                        LethalityDifferenceItemEntity itemEntity5 = new LethalityDifferenceItemEntity();
                        itemEntity5.setCustomIndicatorName(excelVo.getCustomIndicatorName5());
                        itemEntity5.setCustomIndicatorValue(excelVo.getCustomIndicatorValue5());
                        itemEntity5.setCustomIndicatorExplain(excelVo.getCustomIndicatorExplain5());
                        itemList.add(itemEntity5);
                    }

                    lethalityDifferenceEntity.setCustomIndicatorList(itemList);
                    list.add(lethalityDifferenceEntity);
                }

                //保存数据
                if (!PlatformObjectUtils.isEmpty(list)) {
                    for (LethalityDifferenceEntity lethalityDifferenceEntity : list) {
                        res = lethalityDifferenceRepository.save(lethalityDifferenceEntity);
                        if (res) {
                            List<LethalityDifferenceItemEntity> itemList = lethalityDifferenceEntity.getCustomIndicatorList();
                            if (PlatformObjectUtils.isNotEmpty(itemList)) {
                                for (LethalityDifferenceItemEntity itemEntity : itemList) {
                                    itemEntity.setId(UUIDGenerator.getUUID());
                                    itemEntity.setMainId(lethalityDifferenceEntity.getId());
                                    itemEntity.setDelFlag("0");
                                    res = lethalityDifferenceRepository.saveItem(itemEntity);
                                    if (!res) {
                                        break;
                                    }
                                }
                            }
                        }
                    }

                }
            }
        } catch (Exception e) {
            if (e.getMessage().contains("不是合法")) {
                map.put("mess","上传失败! 上传的文件模板有误,请检查表头是否改动！");
            }
            throw e;
        }
        map.put("res",res);
        return map;
    }

    @Override
    public void exportLethalityDifference(HttpServletResponse response, LethalityDifferenceParams lethalityDifferenceParams) {
        String userId = PlatformSessionContext.getUserID();
        String user = sUserService.getSUser(userId);
        SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
        String role = sysRoleService.getRoleByUserId(userId);
        List<Map> maps = JSONObject.parseArray(role, Map.class);//一个人可能有多个角色
        //当前登录人所有的角色
        StringBuilder roles = new StringBuilder();
        for(int i=0;i<maps.size();i++){
            Map map = maps.get(i);
            roles.append(map.get("roleCode")+",");
        }
        String allRole = roles.toString();
        String provinceId = sUser.getProvinceId();//省id
        //获取所有任务编号和任务管理员
        List<TaskVo> taskManagers = fieldSurveyTaskService.getTaskManagers();
        //当前登录人(任务管理员)新建的所有任务
        List<String> taskCodeList = new ArrayList<>();
        if(!PlatformObjectUtils.isEmpty(taskManagers)) {
            for (TaskVo taskVo : taskManagers) {
                String createUser = taskVo.getCreateUser();
                if (createUser.equals(userId)) {
                    taskCodeList.add(taskVo.getTaskNum());
                }
            }
        }
        //国家管理员和业务员有可能 是组长 组员,不可能是省管理员 省业务员
        //省级管理员和业务员有可能 是组长 组员,不可能是国家管理员  国家业务员,也有可能参与了其他省的任务,需要显示省数据和他参与的
        //国家管理员 查看  修改  删除
        List<LethalityDifferenceEntity> lethalityDifferenceEntityList = new ArrayList<>();
        if(allRole.contains(FxfzConstants.YPG_COUN_GL_ROLE)){
            System.err.println("登录人:"+userId+",角色是 国家管理员------");
            lethalityDifferenceEntityList = lethalityDifferenceRepository.queryLethalityDifference(lethalityDifferenceParams);
        }
        //国家业务员  查看 (如果是本人创建的或者组长需要加修改删除)
        else if(allRole.contains(FxfzConstants.YPG_COUN_YW_ROLE)){
            System.err.println("登录人:"+userId+",角色是 国家业务员------");
            lethalityDifferenceEntityList = lethalityDifferenceRepository.queryLethalityDifference(lethalityDifferenceParams);
        }
        //省级管理员(各省查看各省的)  查看  修改  删除
        else if(allRole.contains(FxfzConstants.YPG_PRI_GL_ROLE)){
            System.err.println("登录人:"+userId+",角色是 省级管理员------");
            //获取该省所有数据和管理员参与的
            lethalityDifferenceEntityList = lethalityDifferenceRepository.queryLethalityDifferenceByProvince(lethalityDifferenceParams,userId,provinceId,taskCodeList);
        }
        //省级业务员(各省查看各省的)查看   如果是本人创建的或者组长需要加修改删除)
        else if(allRole.contains(FxfzConstants.YPG_PRI_YW_ROLE)){
            System.err.println("登录人:"+userId+",角色是 省级业务员------");
            //获取该省所有数据和业务员参与的
            lethalityDifferenceEntityList = lethalityDifferenceRepository.queryLethalityDifferenceByProvince(lethalityDifferenceParams,userId,provinceId,taskCodeList);
        }else{
            //本人创建的或者组长:查看 修改 删除,或者是小组组员:查看
            System.err.println("登录人:"+userId+",无国家/省角色------");
            lethalityDifferenceEntityList = lethalityDifferenceRepository.getLethalityDifferenceByTeamMembers(lethalityDifferenceParams,userId,taskCodeList);
        }
        if (lethalityDifferenceEntityList == null || lethalityDifferenceEntityList.size() <= 0) {
            throw new RuntimeException("数据为空,无法导出！");
        }
        List<ExcelVo> excelVoList = new ArrayList<>();
        if (!PlatformObjectUtils.isEmpty(lethalityDifferenceEntityList)) {
            for(LethalityDifferenceEntity lethalityDifferenceEntity : lethalityDifferenceEntityList){
                ExcelVo excelVo = new ExcelVo();
                SpringUtil.copyPropertiesIgnoreNull(lethalityDifferenceEntity, excelVo);
                //根据主表id查询子表自定义数据
                List<LethalityDifferenceItemEntity> itemEntities = lethalityDifferenceRepository.queryItemData(lethalityDifferenceEntity.getId());
                for(int i = 0;i<itemEntities.size();i++){
                    LethalityDifferenceItemEntity itemEntity = itemEntities.get(i);
                    if(i==0){
                        excelVo.setCustomIndicatorName1(itemEntity.getCustomIndicatorName());
                        excelVo.setCustomIndicatorValue1(itemEntity.getCustomIndicatorValue());
                        excelVo.setCustomIndicatorExplain1(itemEntity.getCustomIndicatorExplain());
                    }else if(i==1){
                        excelVo.setCustomIndicatorName2(itemEntity.getCustomIndicatorName());
                        excelVo.setCustomIndicatorValue2(itemEntity.getCustomIndicatorValue());
                        excelVo.setCustomIndicatorExplain2(itemEntity.getCustomIndicatorExplain());
                    }else if(i==2){
                        excelVo.setCustomIndicatorName3(itemEntity.getCustomIndicatorName());
                        excelVo.setCustomIndicatorValue3(itemEntity.getCustomIndicatorValue());
                        excelVo.setCustomIndicatorExplain3(itemEntity.getCustomIndicatorExplain());
                    }else if(i==3){
                        excelVo.setCustomIndicatorName4(itemEntity.getCustomIndicatorName());
                        excelVo.setCustomIndicatorValue4(itemEntity.getCustomIndicatorValue());
                        excelVo.setCustomIndicatorExplain4(itemEntity.getCustomIndicatorExplain());
                    }else if(i==4){
                        excelVo.setCustomIndicatorName5(itemEntity.getCustomIndicatorName());
                        excelVo.setCustomIndicatorValue5(itemEntity.getCustomIndicatorValue());
                        excelVo.setCustomIndicatorExplain5(itemEntity.getCustomIndicatorExplain());
                    }
                }
                excelVoList.add(excelVo);
            }
        }
        try {
            EasyPoiExcelUtil.exportExcel(excelVoList, null, "sheet", ExcelVo.class, "区县致死性差异性信息", response);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public List<LethalityDifferenceEntity> getFinalValue(String taskId) throws UnsupportedEncodingException {
        //根据任务id获取任务地区
        PreAssessTaskEntity preAssessTaskEntity = preAssessTaskService.getById(taskId);
        String taskDistrict = preAssessTaskEntity.getTaskDistrict();
        if(!PlatformObjectUtils.isEmpty(taskDistrict)) {
            List<DistrictVo> list = new ArrayList<>();
            for (String district : taskDistrict.split("、")) {
                int n = district.length() - district.replaceAll("-", "").length();
                List<SysMenuVo> sysMenuList = getSAreaTree("1");//获取所有省
                if (district.contains("北京市") || district.contains("天津市") || district.contains("上海市") || district.contains("重庆市")) {
                    if (n == 0) {
                        for (SysMenuVo sysMenuVo : sysMenuList) {
                            String name = sysMenuVo.getName();
                            if (district.equals(name)) {//省名称
                                List<SysMenuVo> citySysMenu = getSAreaTree(sysMenuVo.getValue());//获取该省下所有市
                                for (SysMenuVo citySysMenuVo : citySysMenu) {
                                    List<SysMenuVo> areaSysMenu = getSAreaTree(citySysMenuVo.getValue());//获取市下所有区县
                                    for (SysMenuVo areaSysMenuVo : areaSysMenu) {
                                        DistrictVo districtVo = new DistrictVo();
                                        districtVo.setProvinceId(sysMenuVo.getValue());
                                        districtVo.setCityId(citySysMenuVo.getValue());
                                        districtVo.setAreaId(areaSysMenuVo.getValue());
                                        list.add(districtVo);
                                    }
                                }
                            }
                        }
                    } else if (n == 1) {
                        String province = district.split("-")[0];
                        String city = district.split("-")[0];
                        String area = district.split("-")[1];
                        String divisionIds = sysAreaService.getIdByCondition(URLEncoder.encode(province, "UTF-8"), URLEncoder.encode(city, "UTF-8"), URLEncoder.encode(area, "UTF-8"));
                        DistrictVo districtVo = new DistrictVo();
                        districtVo.setProvinceId(divisionIds.split("_")[0]);
                        districtVo.setCityId(divisionIds.split("_")[1]);
                        districtVo.setAreaId(divisionIds.split("_")[2]);
                        list.add(districtVo);
                    }
                } else {
                    if (n == 0) {//说明只有省级,直辖市除外
                        for (SysMenuVo sysMenuVo : sysMenuList) {//遍历所有省
                            String name = sysMenuVo.getName();
                            if (district.equals(name)) {//省名称
                                String value = sysMenuVo.getValue();//省id
                                List<SysMenuVo> citySysMenu = getSAreaTree(value);//获取该省下所有市
                                for (SysMenuVo citySysMenuVo : citySysMenu) {
                                    String cityValue = citySysMenuVo.getValue();//市id
                                    List<SysMenuVo> areaSysMenu = getSAreaTree(cityValue);//获取市下所有区县
                                    for (SysMenuVo areaSysMenuVo : areaSysMenu) {
                                        DistrictVo districtVo = new DistrictVo();
                                        districtVo.setProvinceId(sysMenuVo.getValue());
                                        districtVo.setCityId(citySysMenuVo.getValue());
                                        districtVo.setAreaId(areaSysMenuVo.getValue());
                                        list.add(districtVo);
                                    }
                                }
                            }
                        }
                    } else if (n == 1) {//有省市两级
                        String province = district.split("-")[0];//省
                        String city = district.split("-")[1];//市
                        for (SysMenuVo sysMenuVo : sysMenuList) {//遍历所有省
                            String provinceName = sysMenuVo.getName();
                            if (province.equals(sysMenuVo.getName())) {//省名称
                                List<SysMenuVo> citySysMenu = getSAreaTree(sysMenuVo.getValue());//获取该省下所有市
                                for (SysMenuVo citySysMenuVo : citySysMenu) {
                                    if (city.equals(citySysMenuVo.getName())) {
                                        List<SysMenuVo> areaSysMenu = getSAreaTree(citySysMenuVo.getValue());//获取该省下所有区县
                                        for (SysMenuVo areaSysMenuVo : areaSysMenu) {
                                            DistrictVo districtVo = new DistrictVo();
                                            districtVo.setProvinceId(sysMenuVo.getValue());
                                            districtVo.setCityId(citySysMenuVo.getValue());
                                            districtVo.setAreaId(areaSysMenuVo.getValue());
                                            list.add(districtVo);
                                        }
                                    }
                                }
                            }
                        }
                    } else if (n == 2) {
                        String province = district.split("-")[0];//省
                        String city = district.split("-")[1];//市
                        String area = district.split("-")[2];//区县
                        String divisionIds = sysAreaService.getIdByCondition(URLEncoder.encode(province, "UTF-8"), URLEncoder.encode(city, "UTF-8"), URLEncoder.encode(area, "UTF-8"));
                        DistrictVo districtVo = new DistrictVo();
                        districtVo.setProvinceId(divisionIds.split("_")[0]);
                        districtVo.setCityId(divisionIds.split("_")[1]);
                        districtVo.setAreaId(divisionIds.split("_")[2]);
                        list.add(districtVo);
                    }
                }
            }
            //获取区县致死性所有已入库的数据,根据省市区id
            List<LethalityDifferenceEntity> lethalityDifferenceEntities = new ArrayList<>();
            for(int i=0;i<list.size();i++){
                DistrictVo districtVo = list.get(i);
                LethalityDifferenceEntity lethalityDifferenceEntity = lethalityDifferenceRepository.getPutData(districtVo.getProvinceId(),districtVo.getCityId(),districtVo.getAreaId());
                if(!PlatformObjectUtils.isEmpty(lethalityDifferenceEntity)) {
                    if(districtVo.getProvinceId().equals(lethalityDifferenceEntity.getProvinceId()) && districtVo.getCityId().equals(lethalityDifferenceEntity.getCityId()) && districtVo.getAreaId().equals(lethalityDifferenceEntity.getAreaId())){
                        lethalityDifferenceEntities.add(lethalityDifferenceEntity);
                    }
                }
            }
            List<LethalityDifferenceEntity> finalValueList = new ArrayList<>();
            for(LethalityDifferenceEntity lethalityDifferenceEntity : lethalityDifferenceEntities){
                List<LethalityDifferenceItemEntity> lethalityDifferenceItemEntities = lethalityDifferenceRepository.queryItemData(lethalityDifferenceEntity.getId());
                lethalityDifferenceEntity.setCustomIndicatorList(lethalityDifferenceItemEntities);
                finalValueList.add(lethalityDifferenceEntity);
            }
            return finalValueList;
        }
        return null;
    }

    @Override
    public Map<String,Object> checkIsAdopted(String taskId) throws UnsupportedEncodingException {
        Map<String, Object> map = new HashMap<>();
        FieldSurveyTaskEntity fieldSurveyTaskEntity = fieldSurveyTaskRepository.findById(taskId);
        String taskDistrict = fieldSurveyTaskEntity.getTaskDistrict();//任务地区
        String taskNum = fieldSurveyTaskEntity.getTaskNum();
        //获取任务地区所有涉及的区县
        List<DistrictVo> allData = getAllData(taskDistrict);
        //获取该任务下区县致死性中已采纳的
        List<DistrictVo> adoptedData = lethalityDifferenceRepository.getAdoptedByTaskNum(taskNum);
        List<DistrictVo> diff = new ArrayList<>();
        //获取任务中没有采纳的数据,并进行提示
        for(DistrictVo districtVo : allData){
            if (!adoptedData.contains(districtVo)){
                diff.add(districtVo);
            }
        }
        List<DistrictVo> diffData = duplicateRemoval(diff);//去重
        if(!PlatformObjectUtils.isEmpty(diffData)) {
            StringBuilder mess = new StringBuilder("<font size='4px'>"+taskNum+" 不能结束原因:");
            mess.append("<font size='4px'><br>").append("如下区县的致死性差异性指标尚未采集或采纳，请到【现场调查数据管理-区县致死性差异性指标】页面采纳处理。");
            for(DistrictVo districtVo : diff){
                mess.append("<font size='4px'><br>").append(districtVo.getProvince()+"-"+districtVo.getCity()+"-"+districtVo.getArea()).append("</font>");
            }
            map.put("res",false);
            map.put("mess",mess.toString());
            return map;
        }
        map.put("res",true);
        map.put("mess","success");
        return map;
    }

    @Override
    public JSONArray checkPutLethalityDifference(String taskNum) {
        //进行入库
        //获取已入库的区县(不区分任务)
        List<LethalityDifferenceEntity> allPutData = lethalityDifferenceRepository.getAllPutData();//已入库的数据
        Map<String,List<LethalityDifferenceEntity>> listMap1=new HashMap<>();
        for(LethalityDifferenceEntity lethalityDifferenceEntity : allPutData){
            List<LethalityDifferenceItemEntity> itemEntityList = lethalityDifferenceRepository.queryItemData(lethalityDifferenceEntity.getId());
            lethalityDifferenceEntity.setCustomIndicatorList(itemEntityList);//添加自定义数据
            List<LethalityDifferenceEntity> allPutDataList = null;
            if (listMap1.containsKey(lethalityDifferenceEntity.getAreaId())){
                allPutDataList=listMap1.get(lethalityDifferenceEntity.getAreaId());
            }else {
                allPutDataList = new ArrayList<>();
            }
            allPutDataList.add(lethalityDifferenceEntity);
            listMap1.put(lethalityDifferenceEntity.getAreaId(),allPutDataList);
        }
        //获取已采纳,将要入库的数据
        List<LethalityDifferenceEntity> adoptedData = lethalityDifferenceRepository.getAdoptedLethalityDifferenceByTaskNum(taskNum);
        Map<String,List<LethalityDifferenceEntity>> listMap2=new HashMap<>();
        for(LethalityDifferenceEntity lethalityDifferenceEntity : adoptedData){
            List<LethalityDifferenceItemEntity> itemEntityList = lethalityDifferenceRepository.queryItemData(lethalityDifferenceEntity.getId());
            lethalityDifferenceEntity.setCustomIndicatorList(itemEntityList);//添加自定义数据
            List<LethalityDifferenceEntity> adoptedDataList = null;
            if (listMap2.containsKey(lethalityDifferenceEntity.getAreaId())){
                adoptedDataList=listMap2.get(lethalityDifferenceEntity.getAreaId());
            }else {
                adoptedDataList = new ArrayList<>();
            }
            adoptedDataList.add(lethalityDifferenceEntity);
            listMap2.put(lethalityDifferenceEntity.getAreaId(),adoptedDataList);
        }
        JSONArray jsonArray = new JSONArray();
        for(String key1 : listMap1.keySet()){
            if (listMap2.containsKey(key1)){
                List<LethalityDifferenceEntity> list1 = listMap1.get(key1);
                List<LethalityDifferenceEntity> list2 = listMap2.get(key1);
                for (int i=0;i<list1.size();i++){
                    JSONObject json = new JSONObject();//用于存储重复部分 相同的区县的数据
                    json.put("rk",list1.get(i));//已入库的相同数据
                    json.put("cn",list2.get(i));//已采纳的相同数据
                    jsonArray.add(json);
                }
            }
        }
        return jsonArray;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateIsAdoptByTaskNum(String taskNum) {
        //任务下已采纳的数据
        List<LethalityDifferenceEntity> adoptedData = lethalityDifferenceRepository.getAdoptedLethalityDifferenceByTaskNum(taskNum);
        boolean res = false;
        if(!PlatformObjectUtils.isEmpty(adoptedData)) {
            for(LethalityDifferenceEntity lethalityDifferenceEntity : adoptedData){
                res = lethalityDifferenceRepository.updateIsAdopt(lethalityDifferenceEntity.getId(),"2");
            }
        }
        return res;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateIsAdoptById(String rkId, String cnId) {
        boolean res = lethalityDifferenceRepository.updateIsAdopt(rkId, "1");
        res = lethalityDifferenceRepository.updateIsAdopt(cnId, "2");
        return res;
    }

    @Override
    public String getCreateFlag(String taskId) {
        //根据任务id获取任务地区
        PreAssessTaskEntity preAssessTaskEntity = preAssessTaskService.getById(taskId);
        String taskNum = preAssessTaskEntity.getTaskNum();
        String flag = "";
        List<LethalityDifferenceFlagEntity> lethalityDifferenceFlagEntityList = lethalityDifferenceRepository.getDataByTaskCode(taskNum);
        if(!PlatformObjectUtils.isEmpty(lethalityDifferenceFlagEntityList) && lethalityDifferenceFlagEntityList.size() > 0){
            flag = "1";
        }else{
            flag = "0";
        }
        return flag;
    }

    @Override
    public Map<String, Object> appQuerySubmitOrNot(String submitFlag, String taskCode, int curPage, int pageSize,String userId) {
        return lethalityDifferenceRepository.appQuerySubmitOrNot(submitFlag,taskCode,curPage,pageSize,userId);
    }

    private List<SysMenuVo> getSAreaTree(String divisionId) throws UnsupportedEncodingException {
        String jsonStr = sysAreaService.jsonSAreaTree(URLEncoder.encode(divisionId, "UTF-8"));
        Gson gson = CreateGson.createGson();
        Type srItemsStandardType = new TypeToken<List<SysMenuVo>>() {
        }.getType();
        List<SysMenuVo> sysMenuList = gson.fromJson(jsonStr, srItemsStandardType);
        return sysMenuList;
    }

    //去重
    private  List<DistrictVo>  duplicateRemoval(List<DistrictVo> list){
        Set set = new HashSet();
        List<DistrictVo> newList = new ArrayList<>();
        for (Iterator iter = list.iterator(); iter.hasNext();){
            Object next = iter.next();
            if(set.add(next)){
                newList.add((DistrictVo) next);
            }
        }
        list.clear();
        list.addAll(newList);
        return list;
    }

    //去重
    private List<LethalityDifferenceParams> duplicateRemova2(List<LethalityDifferenceParams> list){
        Set set = new HashSet();
        List<LethalityDifferenceParams> newList = new ArrayList<>();
        for (Iterator iter = list.iterator(); iter.hasNext();){
            Object next = iter.next();
            if(set.add(next)){
                newList.add((LethalityDifferenceParams) next);
            }
        }
        list.clear();
        list.addAll(newList);
        return list;
    }

    //获取区县空间数据
    private String getDivisionCodeByAreaId(String areaId){
        String areaCode = sysAreaService.getDivisionCodeById(areaId).replaceAll("\"","");//处理字符串,接口返回的数据多了一組引号
        String geom = lethalityDifferenceRepository.getGeomByCode(areaCode);
        return geom;
    }

    private List<DistrictVo> getAllData(String taskDistrict) throws UnsupportedEncodingException {
        List<DistrictVo> list = new ArrayList<>();
        for(String district : taskDistrict.split("、")){
            int n = district.length() - district.replaceAll("-", "").length();
            List<SysMenuVo> sysMenuList = getSAreaTree("1");//获取所有省
            if (district.contains("北京市")||district.contains("天津市")||district.contains("上海市")||district.contains("重庆市")){
                if(n==0){
                    for(SysMenuVo sysMenuVo : sysMenuList) {
                        String name = sysMenuVo.getName();
                        if (district.equals(name)) {//省名称
                            List<SysMenuVo> citySysMenu = getSAreaTree(sysMenuVo.getValue());//获取该省下所有市
                            for(SysMenuVo citySysMenuVo : citySysMenu){
                                List<SysMenuVo> areaSysMenu = getSAreaTree(citySysMenuVo.getValue());//获取市下所有区县
                                for(SysMenuVo areaSysMenuVo : areaSysMenu){
                                    DistrictVo districtVo = new DistrictVo();
                                    districtVo.setProvince(sysMenuVo.getName());
                                    districtVo.setCity(citySysMenuVo.getName());
                                    districtVo.setArea(areaSysMenuVo.getName());
                                    districtVo.setProvinceId(sysMenuVo.getValue());
                                    districtVo.setCityId(citySysMenuVo.getValue());
                                    districtVo.setAreaId(areaSysMenuVo.getValue());
                                    list.add(districtVo);
                                }
                            }
                        }
                    }
                }else if(n==1){
                    String province = district.split("-")[0];
                    String city = district.split("-")[0];
                    String area = district.split("-")[1];
                    String divisionIds = sysAreaService.getIdByCondition(URLEncoder.encode(province, "UTF-8"), URLEncoder.encode(city, "UTF-8"), URLEncoder.encode(area, "UTF-8"));
                    DistrictVo districtVo = new DistrictVo();
                    districtVo.setProvince(province);
                    districtVo.setCity(city);
                    districtVo.setArea(area);
                    districtVo.setProvinceId(divisionIds.split("_")[0]);
                    districtVo.setCityId(divisionIds.split("_")[1]);
                    districtVo.setAreaId(divisionIds.split("_")[2]);
                    list.add(districtVo);
                }
            }else{
                if(n==0){//说明只有省级,直辖市除外
                    for(SysMenuVo sysMenuVo : sysMenuList){//遍历所有省
                        String name = sysMenuVo.getName();
                        if(district.equals(name)){//省名称
                            String value = sysMenuVo.getValue();//省id
                            List<SysMenuVo> citySysMenu = getSAreaTree(value);//获取该省下所有市
                            for(SysMenuVo citySysMenuVo : citySysMenu){
                                String cityValue = citySysMenuVo.getValue();//市id
                                List<SysMenuVo> areaSysMenu = getSAreaTree(cityValue);//获取市下所有区县
                                for(SysMenuVo areaSysMenuVo : areaSysMenu){
                                    DistrictVo districtVo = new DistrictVo();
                                    districtVo.setProvince(sysMenuVo.getName());
                                    districtVo.setCity(citySysMenuVo.getName());
                                    districtVo.setArea(areaSysMenuVo.getName());
                                    districtVo.setProvinceId(sysMenuVo.getValue());
                                    districtVo.setCityId(citySysMenuVo.getValue());
                                    districtVo.setAreaId(areaSysMenuVo.getValue());
                                    list.add(districtVo);
                                }
                            }
                        }
                    }
                }else if(n==1){//有省市两级
                    String province = district.split("-")[0];//省
                    String city = district.split("-")[1];//市
                    for(SysMenuVo sysMenuVo : sysMenuList) {//遍历所有省
                        String provinceName = sysMenuVo.getName();
                        if (province.equals(sysMenuVo.getName())) {//省名称
                            List<SysMenuVo> citySysMenu = getSAreaTree(sysMenuVo.getValue());//获取该省下所有市
                            for(SysMenuVo citySysMenuVo : citySysMenu) {
                                if(city.equals(citySysMenuVo.getName())){
                                    List<SysMenuVo> areaSysMenu = getSAreaTree(citySysMenuVo.getValue());//获取该省下所有区县
                                    for(SysMenuVo areaSysMenuVo : areaSysMenu){
                                        DistrictVo districtVo = new DistrictVo();
                                        districtVo.setProvince(sysMenuVo.getName());
                                        districtVo.setCity(citySysMenuVo.getName());
                                        districtVo.setArea(areaSysMenuVo.getName());
                                        districtVo.setProvinceId(sysMenuVo.getValue());
                                        districtVo.setCityId(citySysMenuVo.getValue());
                                        districtVo.setAreaId(areaSysMenuVo.getValue());
                                        list.add(districtVo);
                                    }
                                }
                            }
                        }
                    }
                }else if(n==2){
                    String province = district.split("-")[0];//省
                    String city = district.split("-")[1];//市
                    String area = district.split("-")[2];//区县
                    String divisionIds = sysAreaService.getIdByCondition(URLEncoder.encode(province, "UTF-8"), URLEncoder.encode(city, "UTF-8"), URLEncoder.encode(area, "UTF-8"));
                    DistrictVo districtVo = new DistrictVo();
                    districtVo.setProvince(province);
                    districtVo.setCity(city);
                    districtVo.setArea(area);
                    districtVo.setProvinceId(divisionIds.split("_")[0]);
                    districtVo.setCityId(divisionIds.split("_")[1]);
                    districtVo.setAreaId(divisionIds.split("_")[2]);
                    list.add(districtVo);
                }
            }
        }
        return list;
    }

}
